답변:
다음을 좋아할 수 있습니다.
window.open('url', 'window name', 'window settings')
jQuery :
$('a#link_id').click(function(){
window.open('url', 'window name', 'window settings');
return false;
});
또한 설정할 수 target에 _blank실제로.
클릭 핸들러 내에서 타겟을 강제하는 방법은 다음과 같습니다.
$('a#link_id').click(function() {
$(this).attr('target', '_blank');
});
$(this).attr('target', '_blank'); 을 다음으로 변경할 수 있습니다 this.target = "_blank";. 페이지의 앵커 링크가 rel="external"속성 을 갖도록 수정 될 수있는 경우 jQuery 선택기를 사용 a[rel="external"]하는 대신 페이지에 대한 글로벌 클릭 핸들러를 만들 수 있습니다 . 링크 당 처리기를 클릭하면로 선택a#link_id
당신은 사용해야 할 것입니다 window.open(url);
참조 :
http://www.htmlcodetutorial.com/linking/linking_famsupp_120.html
http://www.w3schools.com/jsref/met_win_open.asp
이 문제에 대한 흥미로운 해결책을 찾았습니다. 웹 서비스의 반환을 기반으로 정보를 포함하는 스팬을 만들고있었습니다. 클릭하면 "a"가 클릭을 캡처 할 수 있도록 범위 주위에 링크를 배치하려고 생각했습니다.
하지만 스팬으로 클릭을 캡처하려고했기 때문에 스팬을 만들 때 왜 이렇게하지 않는지 생각했습니다.
var span = $('<span id="something" data-href="'+url+'" />');
그런 다음 'data-href'속성을 기반으로 링크를 생성 한 범위에 클릭 핸들러를 바인딩했습니다.
span.click(function(e) {
e.stopPropagation();
var href = $(this).attr('data-href');
var link = $('<a href="http://' + href + '" />');
link.attr('target', '_blank');
window.open(link.attr('href'));
});
이를 통해 성공적으로 범위를 클릭하고 적절한 URL로 새 창을 열 수있었습니다.
뭐가 문제 야 <a href="myurl.html" target="_blank">My Link</a>? 자바 스크립트가 필요하지 않습니다 ...
이 솔루션은 또한 url이 비어 있고 빈 링크를 비활성화 (회색)하는 경우를 고려했습니다.
$(function() {
changeAnchor();
});
function changeAnchor() {
$("a[name$='aWebsiteUrl']").each(function() { // you can write your selector here
$(this).css("background", "none");
$(this).css("font-weight", "normal");
var url = $(this).attr('href').trim();
if (url == " " || url == "") { //disable empty link
$(this).attr("class", "disabled");
$(this).attr("href", "javascript:void(0)");
} else {
$(this).attr("target", "_blank");// HERE set the non-empty links, open in new window
}
});
}
a.disabled {
text-decoration: none;
pointer-events: none;
cursor: default;
color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<a name="aWebsiteUrl" href="http://www.baidu.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href=" " class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.alibaba.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.qq.com" class='#'>[website]</a>
클릭 이벤트에 대한 이벤트 핸들러 함수 내에서 AJAX 요청 을 실행 하려는 경우주의 하십시오. 어떤 이유로 Chrome (및 기타 브라우저)은 새 탭 / 창을 열지 않습니다.
이것은 아주 좋은 수정은 아니지만 작동합니다.
CSS :
.new-tab-opener
{
display: none;
}
HTML :
<a data-href="http://www.google.com/" href="javascript:">Click here</a>
<form class="new-tab-opener" method="get" target="_blank"></form>
자바 스크립트 :
$('a').on('click', function (e) {
var f = $('.new-tab-opener');
f.attr('action', $(this).attr('data-href'));
f.submit();
});
라이브 예 : http://jsfiddle.net/7eRLb/
Microsoft IE는 두 번째 인수로 이름을 지원하지 않습니다.
window.open('url', 'window name', 'window settings');
문제는 window name입니다. 이것은 작동합니다.
window.open('url', '', 'window settings')
Microsoft는 해당 인수를 사용하는 경우 다음 인수 만 허용합니다.