我有这个脚本,它会在单击任何正文时打开弹出窗口。 在弹出窗口出现之前,如何更改 body 点击应该是按钮点击或链接点击?我需要创建任何按钮吗?非常感谢代码。
<script type="text/javascript">
var win = window.open('http://google.com', "popup", 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
if (!win){
// alert("failed for most browsers");
document.body.onclick = function () {
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
};
}else {
var thisdoc = document;
//Is it Chrome?
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function() {
if ((win.innerHeight > 0) == false){
// alert("failed for chrome");
thisdoc.body.onclick = function () {
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
};
}
}, 100);
}else{
win.onload = function() {
if ((win.innerHeight > 0) == false){
// alert("failed for others");
thisdoc.body.onclick = function () {
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
};
}
};
}
}
</script>
JS: fiddle :http://jsfiddle.net/ayiem999/678Ru/ (使用按钮创建。请播放)
我对这个问题的回答供引用:
<button id="3" onclick="reply_click(this.id)">B3</button>
<script type="text/javascript">
function reply_click(clicked_id)
{
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
}
</script>
最佳答案
如果您的链接或按钮的 ID 为 myButton
,那么它就是
document.getElementById('myButton').onclick = function () {
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
};
这没有 jQuery,因为不需要。如果您想改用它,那就是
$('#myButton').on('click', function(){
window.open('http://google.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
});
关于javascript - 将 body onclick 更改为按钮 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21445918/