使用jquery提取标签之间的值并添加标题属性

发布于 2025-01-02 14:46:37 字数 899 浏览 4 评论 0原文

我在动态生成的页面上有几个 a 标记,如下所示,

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  id='s_ViewBar' name=s_ViewBar>&nbsp;Inventory&nbsp;</a>

我需要遍历其 href 标记中包含文本 "SWEtargetGotoURL" 的所有 a 标记,并根据 ,我需要添加标题标签

例如

if(extractedtext == "&nbsp;Inventory&nbsp;")
   title = "This is inventory homepage";

a 标签最终应该看起来像这样

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  title = "This is inventory homepage" id='s_ViewBar' name=s_ViewBar>&nbsp;Inventory&nbsp;</a>

我使用以下代码来获取 href 标签中具有特定值的所有标签,但我无法获取中间的文本,我不知道如何运行循环并处理所有 a 标签

 var screenName = $('a[href*="SWETargetGotoURL"]').attr('href');

非常感谢任何帮助。

I have several a tags as shown below on a dynamically generated page

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  id='s_ViewBar' name=s_ViewBar> Inventory </a>

I need to traverse through all the a tags containing the text "SWETargetGotoURL" in their href tag and depending on the text I need to add the title tag

for example

if(extractedtext == " Inventory ")
   title = "This is inventory homepage";

The a tag should finally look like this

<a href='JavaScript:SWETargetGotoURL(vtUrl+"XRX+eCustomer+Account+Inventory+View", "_self")'  title = "This is inventory homepage" id='s_ViewBar' name=s_ViewBar> Inventory </a>

I used following code to get all the tags with specific value in href tag but I am unable to get the text in between and I don't know how to run a loop and work on all the a tags

 var screenName = $('a[href*="SWETargetGotoURL"]').attr('href');

Any help is really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2025-01-09 14:46:37

我认为您正在寻找的是 $.each ,它迭代选定元素的集合。

$('a[href*="SWETargetGotoURL"]').each(function () {

    //get the text of the element
    var extractedtext = $(this).text();

    //check text for match
    if (extractedtext == " Inventory ") {

        //Change title attribute of element
        $(this).attr('title', "This is inventory homepage");
    }
});

I think what you're looking for is $.each which iterates over the set of selected elements.

$('a[href*="SWETargetGotoURL"]').each(function () {

    //get the text of the element
    var extractedtext = $(this).text();

    //check text for match
    if (extractedtext == " Inventory ") {

        //Change title attribute of element
        $(this).attr('title', "This is inventory homepage");
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文