【问题标题】:JQuery add attribute to anchor tag in specific div tag with id pattern content classJQuery将属性添加到具有id模式内容类的特定div标签中的锚标签
【发布时间】:2014-08-14 11:20:39
【问题描述】:

谁能告诉我如何将属性 target=_blank 添加到 div 类内容中的锚标记。看起来很简单,但我无法让它工作,我正在获取我需要的 div 中的内容,但无法为其添加属性。 任何帮助将不胜感激。

<div id="row-url_1" class="crm-section url_1-section">
            <div class="label">
                Website
            </div>
             <div class="content">
                <a href="http://www.wesite.com">http://www.website.com ( Home ) </a>
             </div>
             <div class="clear"></div>
          </div>

您可以在以下位置找到演示 http://jsfiddle.net/Lred3to2/

非常感谢

【问题讨论】:

  • &lt;a href="http://www.wesite.com" target="_blank"&gt;http://www.website.com ( Home ) &lt;/a&gt;
  • jsfiddle.net/satpalsingh/27jvqnag 使用 jquery $('.content a').prop('target', '_blank')
  • 我需要锚标记的属性才能在新窗口中打开。不是锚标签的值
  • @Satpal 在我的上下文中我无法到达锚标签,请您在我的小提琴中使用它。我有其他几个 div 标签,不需要在新窗口中打开,只需要在这个 div 块中添加属性。你可以看到我的选择器 cj("div[id*='row-url']")

标签: jquery html


【解决方案1】:

你可以简单地使用

$('.content a').prop('target', '_blank')

DEMO

根据您的代码使用

cj("div[id*='row-url']").find('.content a').prop('target', '_blank');

【讨论】:

  • 尝试了您的代码,但出现错误:Uncaught SyntaxError: Unexpected token ( ... 在此之后它似乎不接受 ("a"):children('.content')
  • @Developer,在哪一行?使用cj("div[id*='row-url']").find('.content a').prop('target', '_blank')
  • 好朋友。多谢。它现在工作正常,遵循你的代码 cj("div[id*='row-url']").find('.content a').prop('target', '_blank');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多