jQuery在新窗口中打开链接无法正常工作
我有一个动态导航,需要在新窗口中打开一个链接。
I have a dynamic navigation and need to open one link in a new window.
因此我使用jQuery来帮助,但下面的代码看起来不像工作:
As such I am using jQuery to help, but the following code does not seem to work:
<script type="text/javascript">
$('a[href="http://globalhealth-dev.oit.duke.edu/education/global-health-courses"]').attr("target", "_blank");
</script>
为自己尝试: http://globalhealth-dev.oit.duke.edu/education/ 然后点击教育下的全球健康课程链接。
To try it for yourself: http://globalhealth-dev.oit.duke.edu/education/ then click on the Global Health Courses link under Education.
我希望得到一些帮助,让它正常工作。
I would appreciate some help getting this to work properly.
谢谢。
脚本的选择部分是错误的,即应该与元素中的href匹配的href。试试这个: $('a [href =/ education / global-health-courses]')。attr(target,_ blank);
The selection part of your script is wrong, namely the href that should match the href in the element. Try with this:
$('a[href="/education/global-health-courses"]').attr("target", "_blank");
另请注意,建议不要使用href选择该链接,因为它比仅使用元素中的id然后使用$(a #myid)。
Also note that it is not recommended to select that link using href as it is slower than just using a id in the element and then using $("a#myid").
还要小心只在文档加载时调用它:
Also be careful to call this only when the document is done loading:
<script type="text/javascript">
$(document).ready(function() {
$('a[href="/education/global-health-courses"]').attr("target", "_blank");
});
</script>