Jquery向span添加文本

时间:2022-07-15 17:54:28
<span class="field-validation-valid" data-valmsg-for="Classifications" data-valmsg-replace="true">
</span>

How can I add text to this span?

如何在此范围内添加文字?

What I tried till now is the following:

我到现在为止尝试的是以下内容:

var msg = $('#[data-valmsg-for="Classifications"]').val();
alert(msg);

Could some one throw idea on this.

有人会对此提出一个想法。

2 个解决方案

#1


4  

Try this

$('span[data-valmsg-for="Classifications"]').text('Your Text'); //It wil add text to element

$('span[data-valmsg-for="Classifications"]').text();//It will get your element text

In your file get like this

在你的文件中得到这样的

var msg = $('span[data-valmsg-for="Classifications"]').text()
alert(msg);

DEMO

#2


2  

You need to use .text()

你需要使用.text()

$("[data-valmsg-for='Classifications']").text("Some Text");

Demo


Note: Above selector will add text to ANY element having a custom attribute of data-valmsg-for with a value of Classifications, so if you want to be that specific, than use span[data-valmsg-for='Classifications']

注意:上面的选择器会向具有data-valmsg-for的自定义属性的任何元素添加文本,其值为Classifications,因此如果您想要特定的,则使用span [data-valmsg-for ='Classifications']

#1


4  

Try this

$('span[data-valmsg-for="Classifications"]').text('Your Text'); //It wil add text to element

$('span[data-valmsg-for="Classifications"]').text();//It will get your element text

In your file get like this

在你的文件中得到这样的

var msg = $('span[data-valmsg-for="Classifications"]').text()
alert(msg);

DEMO

#2


2  

You need to use .text()

你需要使用.text()

$("[data-valmsg-for='Classifications']").text("Some Text");

Demo


Note: Above selector will add text to ANY element having a custom attribute of data-valmsg-for with a value of Classifications, so if you want to be that specific, than use span[data-valmsg-for='Classifications']

注意:上面的选择器会向具有data-valmsg-for的自定义属性的任何元素添加文本,其值为Classifications,因此如果您想要特定的,则使用span [data-valmsg-for ='Classifications']