[英]How to get a substring from the text of an input field and display it to another html element
[英]How to get text of another element to display
您好,我试图在单击p标签时更改ap标签的文本。 目前,我有以下HTML和jQuery
<p class='tile' data-default="Measurable" data-clicked="Some text blah blah." style="text-align: center;">Measurable</p>
<p class='tile' data-default="Unique and
own-able" data-clicked="Some text blah blah." style="text-align: center;">Unique and
own-able</p>
$('.tile').click(function(e) { // when the tile is clicked
$('.tile').not(this).text($(not(this).attr('data-default'))); // Except the clicked tile all are restored to default data
$(this).text($(this).attr('data-clicked')); // The clicked tile gets the replacer text
});
当我单击第一个p标签时,它将更改文本,但是第二个p标签的默认文本将更改为“可测量”。 我知道这是怎么回事,因为它获取的是数据默认值的单击p标签实例,并将其放入第二个实例中。 我试图更改代码以对其进行修复,但最终却破坏了它。
您可以使用.text(function)
将文本设置为其相应的data-default
:
$('.tile').not(this).text(function() {
return $(this).data('default');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.