jQuery,data()不更新数据属性 [英] jQuery, data() not updating data attribute
问题描述
我有下面的一段HTML和关联的jQuery。 html
函数正常工作,但 data
函数根本不影响HTML,我不能我的生活一直都很清楚,浏览器根本没有错误。
HTML
< span id =usernameStatusdata-valid =0> x< / span>
jQuery
data
由AJAX调用返回,它将只会是 true
或 false
。
函数validUsername(data)
{
if(data = =='true'){
$(#usernameStatus)。html(y)。data(valid,1);
} else {
$(#usernameStatus)。html(x)。data(valid,0);
}
}
Per jQuery API文档
.data()方法允许我们以任何类型的数据附加到DOM元素,
.data()
方法不会修改现有的DOM节点,但只能通过调用 $ selector.data()
来获取。您的DOM元素不会改变,只是附加到它们的数据。
I have the below piece of HTML and the associated jQuery. The html
function is working correctly but the data
function isn't affecting the HTML at all, I can't for the life of me figure it out, no errors in the browser at all.
HTML
<span id="usernameStatus" data-valid="0">x</span>
jQuery
data
is returned by an AJAX call, it will only ever be true
or false
.
function validUsername(data)
{
if (data === 'true') {
$("#usernameStatus").html("y").data("valid", 1);
} else {
$("#usernameStatus").html("x").data("valid", 0);
}
}
Per jQuery API Docs
The .data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.
The .data()
method will not modify the existing DOM node, but will only be retrievable via calling of $selector.data()
. Your DOM elements don't change, just the data attached to them.
这篇关于jQuery,data()不更新数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!