JQuery 附加不带 id 或类的标记

JQuery Append Tag Without Id or Class

本文关键字:id JQuery      更新时间:2023-09-26

我有重复的nobr标签,我不能添加ID和类。

如何找到包含文本字符串的特定 nobr 标签?

<nobr>Due Date</nobr>
<nobr>Test</nobr>
<nobr>Hello</nobr>
$(document).ready(function() {
    $('<nobr>Due Date').append('<span class="ms-formvalidation" title="This is a required field." > *</span>');
});

如果我只是有:

 $("nobr").append("*");

那么所有贵族都会受到影响。

$('nobr').filter(function() {
  return $(this).text() == 'Due Date';
});

http://api.jquery.com/contains-selector/

$(document).ready(function() {
  $("nobr:contains('Due Date')").append('<span class="ms-formvalidation" title="This is a required field." > *</span>');
});

工作示例 : http://jsfiddle.net/EnigmaMaster/FkZcY/

$('nobr').get(1); // get the second one

像这样使用 containts 伪选择器

 $("nobr:contains(Test)").append("*");

以下是文档:http://api.jquery.com/contains-selector/

$("nobr").each(function() {
    if( $(this).text() == "hello world" ) {
       // this is the element with the text in it
    }
});

我不是jQuery的人,可能有更好的解决方案。