jQuery 如何选择所有带有特定文本的锚标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2446936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:32:29  来源:igfitidea点击:

How to select all anchor tags with specific text

jqueryhtmlcss-selectors

提问by Geoff Appleford

Given multiple anchor tags:

给定多个锚标签:

<a class="myclass" href="...">My Text</a>

How do I select the anchors matching the class and with some specific text. eg Select all anchors with the class:'myclass' and text:'My Text'

如何选择与类和某些特定文本匹配的锚点。例如选择所有带有 class:'myclass' 和 text:'My Text' 的锚点

回答by David Morton

$("a.myclass:contains('My Text')")

回答by Andy E

You could create a custom selector similar to :containsfor exact matches:

您可以创建一个类似于:contains精确匹配的自定义选择器:

$.expr[':'].containsexactly = function(obj, index, meta, stack) 
{  
    return $(obj).text() === meta[3];
}; 

var myAs = $("a.myclass:containsexactly('My Text')");

回答by karim79

If you are only bothered if the anchor's text containsa particular string, go with @Dave Morton's solution. If, however, you want to exactlymatch a particular string, I would suggest something like this:

如果您只对锚文本包含特定字符串感到困扰,请使用@Dave Morton 的解决方案。但是,如果您想完全匹配特定的字符串,我会建议如下:

$.fn.textEquals = function(txt) {
    return $(this).text() == txt;
}

$(document).ready(function() {
    console.log($("a").textEquals("Hello"));
    console.log($("a").textEquals("Hefllo"))
});

<a href="blah">Hello</a>

Slightly improved version (with a second trimparameter):

稍微改进的版本(带有第二个修剪参数):

$.fn.textEquals = function(txt,trim) {
    var text = (trim) ? $.trim($(this).text()) : $(this).text();
    return text == txt;
}

$(document).ready(function() {
    console.log($("a.myclass").textEquals("Hello")); // true
    console.log($("a.anotherClass").textEquals("Foo", true)); // true
    console.log($("a.anotherClass").textEquals("Foo")); // false
});

<a class="myclass" href="blah">Hello</a>
<a class="anotherClass" href="blah">   Foo</a>

回答by Monika

First, select all tags containing 'MY text'. Then for each exact match, if it matches the condition do what ever you want to do.

首先,选择包含“我的文本”的所有标签。然后对于每个完全匹配,如果它匹配条件做你想做的任何事情。

$(document).ready(function () {
    $("a:contains('My Text')").each(function () {
        $store = $(this).text();

        if ($store == 'My Text') {
            //do Anything.....
        }
    });
});

回答by mmika1000

If you don't know the class of the desired object and just want to go after the link text it is possible to use

如果您不知道所需对象的类并且只想跟踪链接文本,则可以使用

$(".myClass:contains('My Text')")

If you even don't know which element it is (e.g. a, p, link, ...) you can use

如果您甚至不知道它是哪个元素(例如 a、p、link 等),您可以使用

$(":contains('My Text')")

(just leaving the part before :blank.)

(只是在:空白之前留下部分。)

I have to add here that it brings up all elements beginning from <html>-Tag down to the desired element. A solution I could provide is adding .last()to it, but this one only works if there's only a single element to find. Maybe sbdy. knows a better solution here.

我必须在这里补充一点,它将从<html>-Tag开始的所有元素带到所需的元素。我可以提供的一个解决方案是添加.last()到它,但这个只有在只有一个元素可以找到时才有效。也许sbdy。在这里知道更好的解决方案。

Actually, this should be an addition to the accepted answer, especially to @Amalgovinus question.

实际上,这应该是对已接受答案的补充,尤其是对@Amalgovinus 问题。

回答by Muhammad Tanweer

I think this should work for the exact match thing..

我认为这应该适用于完全匹配的事情..

$("a.myclass").html() == "your text"