获得“目标”的价值如果它通过jQuery匹配某个字符串属性 [英] Get value of "target" attribute if it matches some string via jQuery

查看:63
本文介绍了获得“目标”的价值如果它通过jQuery匹配某个字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些链接

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

如何从UniqueString_开始获取target属性的值?元素的ID是未知的,我们需要在target中匹配UniqueString进行搜索

How do I get the value of target attribute starting with "UniqueString_" ? The ID of element is unknown, we need to search by matched "UniqueString" in "target"

我还需要将此值作为链接参数附加到已知元素

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

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


推荐答案

你的第一个请求好像你想找出任何一个目标属性开始的链接具有特定值。如果是这种情况,以下选择器将起作用。你声明你需要完整的目标属性,所以我通过 $。attr()方法将其拉出来。

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");

现在将其附加到另一个链接的HREF值:

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

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

这篇关于获得“目标”的价值如果它通过jQuery匹配某个字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆