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

javascript - jQuery 1.3 - Issue with Setting an Attribute Value - Stack Overflow

programmeradmin1浏览0评论

This is my first stackoverflow question, so try to be nice. ;-D

My issue is this, I am refactoring some existing javascript code and using jQuery to do it. In several places I've e across javascript code similar to the following:

// some js code working with the customAttribute value
javascriptElementObject.customAttribue = void(0);

The javascriptElementObject is now a jQuery object and I have been attempting to use the following code to do the same thing:

// some js code working with the customAttribute value
javascriptElementObject.attr("customAttribute", void(0));

However, this does not seem to be doing anything. The following code works however:

javascriptElementObject.get(0).customAttribute = void(0);

I'm aware of jQuery's removeAttr() function, but have not used it so far because I don't know if it's equivalent to setting the attribute value to void(0).

So I guess that really means I have 2 questions:

  1. Why doesn't the first jQuery version work?
  2. Are .get(0).customAttribue = void(0); and .removeAttr("customAttribute); equivalent?

Thanks.

This is my first stackoverflow question, so try to be nice. ;-D

My issue is this, I am refactoring some existing javascript code and using jQuery to do it. In several places I've e across javascript code similar to the following:

// some js code working with the customAttribute value
javascriptElementObject.customAttribue = void(0);

The javascriptElementObject is now a jQuery object and I have been attempting to use the following code to do the same thing:

// some js code working with the customAttribute value
javascriptElementObject.attr("customAttribute", void(0));

However, this does not seem to be doing anything. The following code works however:

javascriptElementObject.get(0).customAttribute = void(0);

I'm aware of jQuery's removeAttr() function, but have not used it so far because I don't know if it's equivalent to setting the attribute value to void(0).

So I guess that really means I have 2 questions:

  1. Why doesn't the first jQuery version work?
  2. Are .get(0).customAttribue = void(0); and .removeAttr("customAttribute); equivalent?

Thanks.

Share Improve this question edited Sep 22, 2009 at 17:27 Dan Rigby asked Jan 14, 2009 at 22:06 Dan RigbyDan Rigby 17.9k7 gold badges45 silver badges60 bronze badges 2
  • Is there a difference between customAttribue and customAttribute? or just a typo? – John Sheehan Commented Jan 14, 2009 at 22:44
  • It was a typo, fixed it. – Dan Rigby Commented Sep 22, 2009 at 17:27
Add a ment  | 

4 Answers 4

Reset to default 6

jQuery likes to overload its methods so:

obj.attr( name ) //retrieves the attribute value
obj.attr( name, value ) //sets the attribute

obj.attr( name, void(0) ) == obj.attr( name, null ) == obj.attr( name ) //i.e retrieving the attribute

You might want to try the following if you want to set an empty attribute

obj.attr( name, '' )

This will also apply to other methods jQuery.html() for example

What are you trying to acplish?

If the goal is to remove the value in the name/value pair, you might as well just remove the attribute entirely. I'm not aware of any intrinsic value in maintaining an attribute that has no value; in less standards-pliant browsers it may even cause a problem.

In general, the syntax of $(selector).attr(name, value) and $(selector).removeAttr(name) work very well (at least I've never seen it fail.)

If you're trying to use void(0) to keep A HREFs from firing you'd be better off using a "return false" as the click event on those A tags.

The only way to work with custom attributes via jQuery objects is:

obj.get(0).myCustomAttr = 'some value';

That is because jQuery's attr() method will not work with custom attributes (except while applied on a XML-document).

Note also that meouw's answer regarding jQuery overloading functions is not precisely correct, because jQuery checks for the parameters passed to it in such a manner that:

jQuery.funcname(param)

and

jQuery.funcname(param, null)

differ, becacuse null !== undefined. For example:

var params_test = function(a) {
    if (a === undefined) {
        return 'called with no parameters';
    } else {
        return 'called with one parameter: ' + a;
    }
};
params_test(); // results in 'called with no parameters'
params_test(null); // results in 'called with one parameter: null'

Uhmm, try this:

javascriptElementObject.attr("customAttribute", void(0));
var _void = javascriptElementObject.attr("customAttribute");
alert(_void);
发布评论

评论列表(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; } ?>