Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将属性(id)添加到<;文本>;特定值Js/Jquery的标记_Javascript_Jquery - Fatal编程技术网

Javascript 如何将属性(id)添加到<;文本>;特定值Js/Jquery的标记

Javascript 如何将属性(id)添加到<;文本>;特定值Js/Jquery的标记,javascript,jquery,Javascript,Jquery,我有3个文本值,所有的属性都是类名。然而,我只是想给特定的文本添加一些样式:例如 <text text-anchor="start" class="text">Text 1</text> <text text-anchor="start" class="text">Text 2</text> <text text-anchor="start" class="text">Text 3</text> 如果我采用上述方法,则所有

我有3个文本值,所有的属性都是类名。然而,我只是想给特定的文本添加一些样式:例如

<text text-anchor="start" class="text">Text 1</text>
<text text-anchor="start" class="text">Text 2</text>
<text text-anchor="start" class="text">Text 3</text>
如果我采用上述方法,则所有文本标记都设置为“id”。如何将“id”添加到一个?感谢您的帮助。

使用过滤器()

使用

$(“text.text”)。每个(函数(){
如果($(this).text()=='text 2'){
$(this.attr(“id”,“text id”);
}
});

文本1
文本2
文本3

Fiddle::

您的html无效
if($("text.text").text() == 'Text 2'){
   $("text.text").attr("id", "text-id");
}
$("text.text").filter(function () {
    return $(this).text() == 'Text 2'
}).attr("id", "text-id");
$('.text').each(function(){
    if($(this).text() == "Text 2"){
        $(this).attr('id','CustomID');
    }
});