使用特定数据属性更改所有元素的文本 [英] Change text of all elements with specific data-attribute
本文介绍了使用特定数据属性更改所有元素的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试使用jQuery将多个元素的文本更改为特定数据属性的值。
I'm currently trying to use jQuery to change the text of a number of elements to the value of a specific data-attribute.
例如:
index.html
----------
<h1 data-change="text2">text1</h1>
<p data-change="paragraph2">paragraph1</p>
我想将'text1'更改为'text2',将'paragraph1'更改为'paragraph2'(该特定元素的数据变化值。)
I want to change 'text1' to 'text2' and 'paragraph1' to 'paragraph2' (the value of data-change for that specific element).
我将如何做到这一点?
I假设我能够用数据属性'data-change''选择'所有元素然后只做一个简单的 $(this).text($(this).data('改变'));
或其他东西。
I assumed I would be able to 'select' all of the elements with the data-attribute 'data-change' and then just do a simple $(this).text($(this).data('change'));
or something.
推荐答案
你可以这样做:
$('[data-change]').each(function(){
$(this).text($(this).data('change'));
});
您可以迭代元素并更改其文本。
You can iterate over the elements and change its text.
这篇关于使用特定数据属性更改所有元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文