最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - create attribute in js triggers an exception 'not a function' - Stack Overflow

programmeradmin1浏览0评论

I'm using js to change the contents of a div that contents inputs i want to use them with Ajax , I've used Firefox scratchpad to debug this function:

function show(id){
var div = document.getElementById('');
div.innerHTML = '';
var input1 = document.createElement('input')
            .createAttribute('type').setAttribute('type', 'hidden')
            .createAttribute('value').setAttribute('value',id )
            .setAttribute('name','id' );
var input2 = document.createElement('input')
             .createAttribute('type').setAttribute('type', 'input')
             .createAttribute('name').setAttribute('name', '');
var btn = document.createElement('button')
          .createAttribute('onClick').setAttribute('onClick', 'post()');
btn.innerHTML = 'Comment';
div.appendChild(input1).appendChild(input2).appendChild(btn);
}

and what i got is this:

/*
Exception: document.createElement(...).createAttribute is not a function
@Scratchpad/2:2
*/

I understood nothing, any ideas?

I'm using js to change the contents of a div that contents inputs i want to use them with Ajax , I've used Firefox scratchpad to debug this function:

function show(id){
var div = document.getElementById('');
div.innerHTML = '';
var input1 = document.createElement('input')
            .createAttribute('type').setAttribute('type', 'hidden')
            .createAttribute('value').setAttribute('value',id )
            .setAttribute('name','id' );
var input2 = document.createElement('input')
             .createAttribute('type').setAttribute('type', 'input')
             .createAttribute('name').setAttribute('name', '');
var btn = document.createElement('button')
          .createAttribute('onClick').setAttribute('onClick', 'post()');
btn.innerHTML = 'Comment';
div.appendChild(input1).appendChild(input2).appendChild(btn);
}

and what i got is this:

/*
Exception: document.createElement(...).createAttribute is not a function
@Scratchpad/2:2
*/

I understood nothing, any ideas?

Share asked Mar 23, 2013 at 10:55 Majed DHMajed DH 1,30119 silver badges36 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

I believe .createAttribute() belongs to document, not to individual elements, so that would explain that error: .createElement() returns an element, and that element has no function .createAttribute().

But you don't need to use .createAttribute() before calling .setAttribute(), because the latter will create element attributes if they don't already exist. However, I think .setAttribute() returns undefined so you can't really chain it. Try doing it one step at a time:

var input1 = document.createElement('input');
input1.setAttribute('type', 'hidden');
input1.setAttribute('value',id );
input1.setAttribute('name','id' );
// etc.

Basically, the exception says that there is no function called "createAttribute". And that is correct:

.createAttribute() is a function of document: https://developer.mozilla/en-US/docs/DOM/document#Methods

So the functions cannot be chained like you try to do it. You have to call them individually. And anyway, "createAttribute" should not be used anymore (see Using createAttribute vs. just setting the attribute directly?).

function show(id){
    var div = document.getElementById('');
    div.innerHTML = '';

    var input1 = document.createElement('input');
    input1.setAttribute('type', 'hidden');
    input1.setAttribute('value',id );
    input1.setAttribute('name','id' );

    var input2 = document.createElement('input');
    input2.setAttribute('type', 'input');
    input2.setAttribute('name', '');

    var btn = document.createElement('button');
    btn.setAttribute('onClick', 'post()');
    btn.innerHTML = 'Comment';

    div.appendChild(input1).appendChild(input2).appendChild(btn);
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>