gpt4 book ai didi

javascript - window.open 有时会打开链接,有时不会

转载 作者:行者123 更新时间:2023-11-28 19:20:09 25 4
gpt4 key购买 nike

我有一个像这样的按钮:

<button class="facebook text-white" href='http://www.facebook.com/sharer.php?u=url.com'>
<img src="{{ asset('assets/img/facebook.png') }}">
<span>{{"share" | trans}}</span>
</button>

我有一个名为social.js的js文件,其中包含以下内容:

$(document).ready(function(){
$(".twitter").click(function(e) {
e.preventDefault();
var href = $(e.target).attr('href');
window.open(href, "Share your stats on Twitter!", "height=300,width=550")
});

$(".facebook").click(function(e) {
e.preventDefault();
var href = $(e.target).attr('href');
window.open(href, "Share you stats on Facebook!", "height=300,width=550")
});
});

这会导致奇怪的行为,因为有时指定的链接会加载并打开,有时它只是停留在 about:blank 上。

每次点击后,是否打开链接似乎完全是随机的。

如何解决这种不稳定的行为?

最佳答案

使用jQuery UI dialog相当简单:(动态)创建一个 div 并调用 $(div).dialog():

$('.dialog-button').click(function (ev) {
ev.preventDefault();
$('<div/>', {
title: 'Your facebook link here!',
css: {
position: 'relative'
},
append: $('<iframe/>', {
src: $(ev.target).data('href'),
css: {
position: 'absolute',
left: 0, right: 0,
top: 0, bottom: 0,
width: '100%', height: '100%',
border: 0
}
})
}).dialog({
modal: true, resizable: false,
height: 200, width: 350
});
});
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"/>
</head>
<body>
<button class="dialog-button" data-href="http://example.com">Click me!</button>
</body>

关于javascript - window.open 有时会打开链接,有时不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com