[英]Opening links in a new tab with javascript stops working
I have some code on my tumblr blog that makes it so the links in the "notes" div open in a new tab — it works fine but once i click "Show More Notes" (which loads more links), it stops working on those links. 我在tumblr博客上有一些代码,使其能够在新标签中打开“笔记” div中的链接-可以正常工作,但是一旦我单击“显示更多笔记”(加载更多链接),它将停止处理这些链接链接。 Here is my code.
这是我的代码。
<script type="text/javascript">
$(function(){
$("#notes a").attr("target","_blank");
});
</script>
Here is what tumblr says on this issue : "Notes are paginated with AJAX. If your theme needs to manipulate the Notes markup or DOM nodes, you can add a Javascript callback that fires when a new page of Notes is loaded or inserted" 这是tumblr在此问题上所说的 :“注释使用AJAX分页。如果您的主题需要操纵Notes标记或DOM节点,则可以添加Java回调,该注释在加载或插入新的Notes页面时触发”
tumblrNotesLoaded(notes_html) "If this Javascript function is defined, it will be triggered when a new page of Notes is loaded and ready to be inserted. If this function returns false, it will block the Notes from being inserted." tumblrNotesLoaded(notes_html) “如果定义了此Javascript函数,则在加载新的Notes页面并准备插入时将触发它。如果此函数返回false,它将阻止Notes插入。”
tumblrNotesInserted() "If this Javascript function is defined, it will be triggered after a new page of Notes has been inserted into the DOM." tumblrNotesInserted() “如果定义了此Javascript函数,则在将新的Notes页面插入DOM后将触发它。”
the newly loaded links wont have target set to _blank ... what you should do, when you've loaded the new links is execute 新加载的链接不会将目标设置为_blank ...在加载新链接后应执行的操作
$("#notes a").attr("target","_blank");
again - as you've shown no code regarding the loading of these links, that's the best I can do 再次-正如您未显示有关这些链接的加载的代码一样,这是我能做到的最好
edit: I just looked at your page - I think this should do the trick 编辑:我只是看过您的页面-我认为这应该可以解决问题
this.style.display = 'none';
document.getElementById('notes_loading_121152690941').style.display = 'block';
if (window.ActiveXObject) var tumblrReq = new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest) var tumblrReq = new XMLHttpRequest();
else return false;
tumblrReq.onreadystatechange = function() {
if (tumblrReq.readyState == 4) {
var notes_html = tumblrReq.responseText.split('<!-- START ' + 'NOTES -->')[1].split('<!-- END ' + 'NOTES -->')[0];
if (window.tumblrNotesLoaded)
if (tumblrNotesLoaded(notes_html) == false) return;
var more_notes_link = document.getElementById('more_notes_121152690941');
var notes = more_notes_link.parentNode;
notes.removeChild(more_notes_link);
notes.innerHTML += notes_html;
if (window.tumblrNotesInserted) tumblrNotesInserted(notes_html);
// ************************************************
$("#notes a").attr("target","_blank");
// ************************************************
}
};
tumblrReq.open('GET', '/notes/121152690941/lhtXynZtK?from_c=1433902641', true);
tumblrReq.send();
return false;
the added line is surrounded by // ************************************************
添加的行被
// ************************************************
包围// ************************************************
You could set target
attribute on mousedown
delegated event, eg: 您可以在
mousedown
委托事件上设置target
属性,例如:
$(function () {
$('#notes').on('mousedown', 'a:not([target])', function () {
this.target = "_blank";
});
});
Use this: 用这个:
$('ol.notes').on('click', 'a', function (e) {
if (e.which > 1 || e.shiftKey || e.altKey || e.metaKey || e.isDefaultPrevented()) {
return;
}
window.open(this.href);
e.preventDefault();
});
This attaches an event handler on the notes container, making sure that it only gets clicks on a
elements and that the clicks are not middle-clicks, alt-clicks, … 这种高度的注意事项容器上的事件处理程序,确保它只是变得点击了
a
元素,而且点击不是中间点击,ALT-点击,...
Once all of those superfluous clicks are filtered out, it simply opens a new tab via Javascript; 一旦所有这些多余的点击都被过滤掉,它就可以通过Javascript打开一个新标签; there's no need to set
target="_blank"
无需设置
target="_blank"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.