Javascript jQuery - 查找包含值的 href 并返回完整值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35084227/
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-23 17:15:52  来源:igfitidea点击:

jQuery - Find href that contains a value and return the full value

javascriptjquery

提问by aphextwig

I'd like to search the body for an href that contains /test/. I then want to return the entire href value e.g. /test/123/.

我想在正文中搜索包含/test/. 然后我想返回整个 href 值,例如/test/123/

Below is what I've come up with so far. I know that it can find /test/but I'm struggling with returning the full href value. Any ideas?

以下是我到目前为止所想出的。我知道它可以找到,/test/但我正在努力返回完整的 href 值。有任何想法吗?

function() {
    var htmlString = $('body').html().toString();
    var index = htmlString.indexOf("/test/");
    if (index != -1)
        return index.closest('div').find('a').attr('href');
}

HTML:

HTML:

<div>
    <a href="/test/123/">Test</a>
</div>

Thanks in advance.

提前致谢。

回答by Ibrahim Khan

You can do it like following using jQuery contain attributeselector.

您可以像使用 jQuery包含属性选择器一样执行以下操作。

$('a[href*="/test/"]').attr('href');

回答by gurvinder372

use starts withselector

使用选择器开头

$( "a[href='^/test/'" ).attr( "href" )

回答by Neil Atkinson

You're using jQuery so use the "attribute contains selector" (https://api.jquery.com/attribute-contains-selector/) and retrieve the href values using "attr" (http://api.jquery.com/attr/).

您正在使用 jQuery,因此请使用“属性包含选择器”(https://api.jquery.com/attribute-contains-selector/)并使用“attr”(http://api.jquery.com)检索 href 值/属性/)。

The following line of code will return all aelements with an href containing the text "/test/".

以下代码行将返回a带有包含文本"/test/"的 href 的所有元素。

var matchingElements = $("a[href*='/test/']")

...and the following code will return an array of their href attributes.

...并且以下代码将返回其 href 属性的数组。

var hrefs = matchingElements.map(function(index, element) {
    return $(element).attr('href');
});

回答by Rupert

Open your console and run this. It will log out all of the links with "/test/" in. You said that you were looking for links that contain"/test/". The other answers I have seen only match for links startingwith "/test/".

打开您的控制台并运行它。它将注销所有带有“/test/”的链接。您说您正在寻找包含“/test/”的链接。我看到的其他答案只匹配以“/test/”开头的链接。

Bear in mind that this will pull out all of the matching links, not just one so you will be returned an array.

请记住,这将拉出所有匹配的链接,而不仅仅是一个,因此您将返回一个数组。

const $links = $("a").filter((index, anchor) => $(anchor).attr('href').match(/\/test\//));
const hrefs = $.map($links, link => $(link).attr('href'));
console.log(hrefs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/test/123">link 1</a>
<a href="/test/456">link 2</a>
<a href="/123">link 3</a>
<a href="/456">link 4</a>
<a href="/test/789">link 5</a>

回答by suraj rawat

You can use CSS “Substring Matching Attribute Selectors”to search the word in a string .

您可以使用 CSS “子字符串匹配属性选择器”来搜索字符串中的单词。

  $("body a[href='^/test/'").attr("href")

[att] Represents an element with the att attribute, whatever the value of the attribute.

[att] 表示具有 att 属性的元素,无论该属性的值如何。

[att=val] Represents an element with the att attribute whose value is exactly "val".

[att=val] 表示一个元素,其属性值为“val”。

[att~=val] Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

[att~=val] 表示具有 att 属性的元素,其值为以空格分隔的单词列表,其中一个正好是“val”。如果“val”包含空格,它将永远不会代表任何内容(因为单词由空格分隔)。此外,如果“val”是空字符串,它永远不会代表任何东西。

[att|=val] Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.

[att|=val] 表示一个具有 att 属性的元素,它的值要么正好是“val”,要么以“val”开头,后面紧跟“-”(U+002D)。这主要是为了允许语言子代码匹配(例如,HTML 中 a 元素上的 hreflang 属性),如 BCP 47([BCP47])或其后续版本中所述。对于 lang(或 xml:lang)语言子代码匹配,请参见 :lang 伪类。

Reference : Reference link

参考:参考链接