javascript - jQuery,data()不更新数据属性

标签 javascript jquery html ajax

<分区>

我有下面的一段 HTML 和关联的 jQuery。 html 函数工作正常,但 data 函数根本不影响 HTML,我这辈子都弄不明白,没有错误浏览器。

HTML

<span id="usernameStatus" data-valid="0">x</span>

jQuery

data 由 AJAX 调用返回,它只会是 truefalse

function validUsername(data)
{        
    if (data === 'true') {
        $("#usernameStatus").html("y").data("valid", 1);
    } else {
        $("#usernameStatus").html("x").data("valid", 0);
    }
}

最佳答案

根据 jQuery API 文档

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.

.data() 方法不会修改现有的 DOM 节点,但只能通过调用 $selector.data()。您的 DOM 元素不会改变,只是附加到它们的数据。

关于javascript - jQuery,data()不更新数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287990/

相关文章:

javascript - jQuery 滚动函数粘性标题应该滚动

javascript - 将鼠标悬停在一个 div 上会触发其他 div 悬停在其中

jquery - 当我调用操作时调用 serveResource

javascript - 我如何停止 bjqs slider

javascript - 如何一键切换两个图像(html/js)

html - 标题元素未在 css 中正确定位

javascript - 如何在 firefox 插件后台保持持续的 websocket 连接

javascript - 有没有办法阻止在最小宽度屏幕上加载内容

使用 .live 帮助 jquery 将鼠标悬停在表行上

javascript - 使用 php 使用 href 链接将输入文本框值传递到另一个页面