我有一个WP博客页面,显示使用类别短代码的最近帖子在该页面上,我有一个链接列表,通向他们的帖子 . 我无法编辑链接以向其添加ID或类,因此我手动使用div来包装短代码和javascript,这使得链接在单击时打开到新选项卡,有没有办法可以打开所有链接到新标签打开单击? (记住,我无法编辑链接)这是我的代码
<!--- i cant edit the links below ----->
<div id="boss" class="boss">
<a href="http://example.com/example-1" title="Example 1">Example 1</a>
<a href="http://example.com/example-1" title="Example 1">Example 2</a>
<a href="http://example.com/example-1" title="Example 1">Example 3</a>
<a href="http://example.com/example-1" title="Example 1">Example 4</a>
</div>
JAVASCRIPT
<script type="text/javascript">
window.onload = function(){
var a = document.getElementById('boss').getElementsByTagName('a');
for (var i=0; i<a.length; i++){
a[i].setAttribute('target', '_blank');
}
}
</script>
我试图在点击时加载所有链接到他们的新标签
<div onclick="boss();">Something To Click On</div>
它不工作,任何帮助PLS?
2 回答
全码示例:
使用jQuery会很容易 . 您可以使用
.each()
和.attr()
来实现它 .NOTE 在WordPress中尝试此脚本 . 由于以下输出是iframe,它将无法正常工作!
祝好运 :)