将_top添加到特定div-jQuery内的标记
例如,我在几个嵌套的div标记中有几个a标记将_top添加到特定div-jQuery内的标记,jquery,html,attr,Jquery,Html,Attr,例如,我在几个嵌套的div标记中有几个a标记 <div id="top_level_div"> <div id="inner_level_div"> <a href="http://yahoo.com>text</a> </div> <div id="inner_level_div1> <a href="http://yahoo.com>text</a
<div id="top_level_div">
<div id="inner_level_div">
<a href="http://yahoo.com>text</a>
</div>
<div id="inner_level_div1>
<a href="http://yahoo.com>text</a>
</div>
</div>
像这样:
$("#top_level_div a").attr("target","_top");
#top_level_div a
将获得ID=top_level_div的元素的所有锚定标记后代:
$('#top_level_div a').attr('target', '_top');
像这样的东西怎么样:
$("#top_level_div a").attr("target","_top");
.attr()的文档
:试试看
我应该在哪里添加此代码。我尝试在不同的地方添加它,但似乎不起作用
$('#top_level_div a').attr('target', '_top');
$("#top_level_div a").attr("target","_top");
//select the root element
//then find all the `a` elements that are descendants of the root element
//then change that set of elements `target` attribute to `_top`
$('#top_level_div').find('a').attr('target', '_top');
$("#top_level_div a").attr("target","_top")
$('#top_level_div').find('a').attr('target', '_top');