[英]Can't set element ID in JavaScript, it's always undefined
I want to have a function that generates ID's on the fly for a given jquery object, if it doesn't have one already. 我想要一个为给定的jquery对象动态生成ID的函数,如果它还没有的话。 These ID's should then be used in future requests.
然后,这些ID应在以后的请求中使用。
I came up with the code below, but it doesn't work. 我想出了下面的代码,但是没有用。 The ID's are never set.
永远不会设置ID。 The commented out alert statement below always return undefined.
下面注释掉的警报语句始终返回undefined。
I always pass code like $(this) or $(options.el) as a parameter to substitute 'el'. 我总是将$(this)或$(options.el)之类的代码作为参数替换“ el”。 Initially, the elements do not have explicitly ID set in HTML.
最初,元素没有在HTML中明确设置ID。
Any help would be appreciated, here's the code: 任何帮助将不胜感激,这是代码:
getElementId: function(el)
{
if(undefined == el.attr('id'))
{
el.attr('id',"anim-"+Math.random().toString().substr(2));
}
// alert(el.attr('id'));
return el.attr('id');
},
Test the truthiness of the valued returned by .attr()
, and make sure that el
is actually a jQuery object. 测试
.attr()
返回的值的真实性,并确保el
实际上是一个jQuery对象。
getElementId: function(el)
{
if (!el.jquery)
{
el = $(el);
}
if(!el.attr('id'))
{
el.attr('id',"anim-"+Math.random().toString().substr(2));
}
// alert(el.attr('id'));
return el.attr('id');
},
Depending on the attribute you're trying to retrieve, .attr()
may return undefined
or ""
. 根据您尝试检索的属性,
.attr()
可能返回undefined
或""
。
Other recommended cleanup: 其他建议清除:
getElementId: function(el)
{
if (!el.jquery)
{
el = $(el);
}
var id = el.attr('id');
if(!id)
{
// make sure this ID hasn't actually been used before!
do
{
id = "anim-"+Math.random().toString().substr(2);
} while (!$('#' + id).length);
el.attr('id', id);
}
return id;
},
You really need to make sure that you're not assigning an ID to that element which has already been used before. 您确实需要确保没有为该元素分配一个ID,该ID之前已经被使用过。 The above function does this.
上面的功能可以做到这一点。 A simpler way to accomplish the same thing is to use an incrementing counter stored somewhere outside of the scope of the function:
完成同一件事的一种更简单的方法是使用存储在函数范围之外的地方的递增计数器:
__idCounter__: 1,
getElementId: function(el)
{
if (!el.jquery)
{
el = $(el);
}
var id = el.attr('id');
if(!id)
{
id = "anim-" + (this.__idCounter__++);
el.attr('id', id);
}
return id;
},
There seems to be confusion about what .attr()
returns when an element does not have the attribute requested. 当元素没有请求的属性时,
.attr()
返回什么似乎有些混乱。 After running some tests , it looks like .attr()
returns: 运行一些测试后 ,
.attr()
返回:
id
and class
(perhaps other HTML4-spec attributes as well; didn't test) id
和class
的空字符串(也许还有其他HTML4-spec属性;未测试) undefined
for other attributes not defined by the spec undefined
的其他属性undefined
Output from my tests : 我的测试输出:
<div id="myID"/> id: myID
<div id="myID"/> class:
<div id="myID"/> data-attr: undefined
<div id="myID"/> asdf: undefined
<div class="myClass"/> id:
<div class="myClass"/> class: myClass
<div class="myClass"/> data-attr: undefined
<div class="myClass"/> asdf: undefined
<div data-attr="myAttr"/> id:
<div data-attr="myAttr"/> class:
<div data-attr="myAttr"/> data-attr: myAttr
<div data-attr="myAttr"/> asdf: undefined
<div asdf="myNonSpecAttr"/> id:
<div asdf="myNonSpecAttr"/> class:
<div asdf="myNonSpecAttr"/> data-attr: undefined
<div asdf="myNonSpecAttr"/> asdf: myNonSpecAttr
Just test the truthiness of the value returned by .attr()
, since that treats undefined
and ""
(the empty string) the same: 只需测试
.attr()
返回的值的真实性,因为它将undefined
和""
(空字符串)视为相同:
if (somejQueryElt.attr('someAttr'))
{
// it definitely has the attribute
}
else
{
// it definitely does not have the attribute
}
Try changing: 尝试更改:
if(undefined == el.attr('id'))
to: 至:
if("" == el.attr('id'))
To find any element/attribute exists in DOM you need to use length
property : Proper condition is : 要查找DOM中存在的任何元素/属性,您需要使用
length
属性:合适的条件是:
if (el.attr('id').length > 0 )
Full function: 功能齐全:
getElementId: function (el) {
return el.attr('id').length > 0 ? el.attr('id') : el.attr('id', "anim-" + Math.random().toString().substr(2));
}
Adding a simple debugging statement in your code would show you why no id is being set. 在代码中添加简单的调试语句将向您显示为什么未设置id。
getElementId: function(el)
{
alert(el.attr('id'));
if(console)console.log(el.attr('id'));
if(undefined == el.attr('id'))
{
el.attr('id',"anim-"+Math.random().toString().substr(2));
}
// alert(el.attr('id'));
return el.attr('id');
},
You will see the alert does not return undefined. 您将看到警报未返回未定义。 It returns an empty string
它返回一个空字符串
getElementId: function(el)
{
var id = el.attr('id');
if(id.length===0)
{
id="anim-"+Math.random().toString().substr(2);
el.attr('id',id);
}
return id;
},
I also hope you know that math.random is really not random. 我也希望您知道math.random实际上不是随机的。 You should have some sort of counter.
您应该有某种计数器。
也许您的情况应该是这样的:
if (typeof el.attr('id') === 'undefined')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.