[英]HTML isn't changed when removing data item
I'm trying to make the data-title
value empty strings for the second option
(where value="24"
). 我正在尝试为第二个option
(其中value="24"
)将data-title
值设置为空字符串。 Although the console.log
statements say the data-title
is now empty, when I inspect the element on my browser, the text is still there. 尽管console.log
语句说data-title
现在为空,但是当我在浏览器上检查元素时,文本仍然存在。 Is there a way to dynamically change the HTML on the browser? 有没有办法在浏览器中动态更改HTML?
HTML: HTML:
<select name="ctl00$phBody$ddlTimeScale" id="ctl00_phBody_ddlTimeScale" class="timeline_options" data-list-items-tooltip-enabled="true">
<option value="1">1 Hour</option>
<option value="24" data-title="text 24">1 Day</option>
<option selected="selected" value="144" data-title="text 144">1 Week</option>
</select>
jQuery/JavaScript: jQuery的/ JavaScript的:
var selectID = "ctl00_phBody_ddlTimeScale";
console.log("Before 24: " + $('#' + selectID + ' option[value="24"]').data('title'));
$('#' + selectID + ' option[value="24"]').data('title', '');
console.log("After 24: " + $('#' + selectID + ' option[value="24"]').data('title'));
除此之外,您可以使用.attr()将数据标题设置为空值。
$('#' + selectID + ' option[value="24"]').attr('data-title', '');
JSFIDDLE DEMO -> http://jsfiddle.net/sp03musf/1/ JSFIDDLE演示-> http://jsfiddle.net/sp03musf/1/
.data() stores data on the matched elements which is different from the attributes data-attr
and .removeData removes the data attribute from the storage. .data()在与属性data-attr
不同的匹配元素上存储数据, .removeData从存储中删除数据属性。
So, basically when you want to remove the attribute, the .data('title')
will still be intact since it comes from the client side storage. 因此,基本上,当您要删除该属性时, .data('title')
仍然是完整的,因为它来自客户端存储。 Use .removeData()
to remove that too. 使用.removeData()
可以删除它。
var selectId = $('#' + selectID + ' option[value="24"]');
selectId.attr('data-title', ''); // empty it in the DOM
selectId.removeData('title'); // clear storage
Note: 注意:
If you don't intend to use localStorage, don't write data('title')
at all. 如果您不打算使用localStorage,则完全不要写入data('title')
。 Just use .attr(data-title)
every single time, so you don't have to removeData()
. 只需每次使用.attr(data-title)
,因此您不必removeData()
。
console.log("Before 24: " + $('#' + selectID + ' option[value="24"]').attr('data-title'));
$('#' + selectID + ' option[value="24"]').attr('data-title', '1');
console.log("After 24: " + $('#' + selectID + ' option[value="24"]').attr('data-title'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.