开发者

Get value of "target" attribute if it matches some string via jQuery

开发者 https://www.devze.com 2022-12-16 08:34 出处:网络
let\'s say I have some links <a href=\"#\" target=\"UniqueString_black\"></a> <a href=\"#\" target=\"dog\"></a>

let's say I have some links

<a href="#" target="UniqueString_black"></a>
<a href="#" target="dog"></a>
<a href="#" target="duck"></a>

How do I get the value of target attribute starting with "UniqueString_" ? The ID of element is unknown, we need to search by matched "UniqueSt开发者_Python百科ring" in "target"

I also need to attach this value as link parameter to a known element

<a id="myID" href="someurl&color=black"></a>


Your first request seems like you want to seek out any link whose target attribute begins with a specific value. If that is the case, the following selector will work. You stated that you needed the full target attribute, so I'm pulling that out via the $.attr() method.

var fullTarget = $("a[target^='UniqueString_']").attr("target");

Now as for appending that to the HREF value of another link:

var link = $("a#myID");
$(link).attr("href", $(link).attr("href") + "&color=" + fullTarget);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号