JQuery数据选择器不会使用.data进行更新 [英] JQuery data selector not updating with .data

查看:125
本文介绍了JQuery数据选择器不会使用.data进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,如果我有一个div加载到具有 data-test 属性的页面上,并使用jquery的 .data(' test')我不能再使用 $('div [data-test =newValue]')

选择元素。

见此处测试:

http://jsfiddle.net/VNaFs/1/

解决方案

jQuery .data()最初由来自 data - 属性的值填充,但设置它仅将相关的新值存储在内存中。它不会更改DOM中的属性。要更改属性,您必须使用:

  $('#one,#three')。attr('data-测试','改变'); 

文档位于 http://api.jquery.com/jQuery.data/


Basically if I have a div loaded onto a page with a data-test attribute and change the value of it with jquery's .data('test') I can no longer select the element with $('div[data-test="newValue"]')

See test here:

http://jsfiddle.net/VNaFs/1/

解决方案

jQuery .data() is initially populated with values from the data- attributes, but setting it only stores the associated new value in memory. It doesn't change the attribute in the DOM. To change the attribute, you have to use:

$('#one, #three').attr('data-test', 'changed');

The docs are at http://api.jquery.com/jQuery.data/

这篇关于JQuery数据选择器不会使用.data进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆