『葉狂人』的怕失憶筆記 Front-End

讓特定的 a tag 變成開新視窗 target=_blank

這個方式,尤其在 Blog 系統上特別好用,因為你無法掌握輸入者都能夠將 a tag 的屬性 target 設成 _blank

$(".opennew").on('click', 'a', function (event) {
	if ($(this).attr('href').indexOf('javascript') == -1) {
		$(this).attr('target','_blank');
		if (event.stopPropagation) {
			event.stopPropagation();
		}
	}
});

以上的 Code 可以讓 class=opennew 的區域內所有的 a tag 點選之後以開新視窗的方式打開 URL

不必更改內容本身的 target 屬性,無論是有沒有設定都強制開新視窗,是不是相當方便

『葉狂人』的怕失憶筆記, author
好奇心激發新想法、新工作及新產業。Curiosity leads to new ideas, new jobs, new industries.
美國企業家 施薇尼 Anne Sweeney