[英]Open href link in popup
I'm trying to open the link in popup, i've write this static html code : 我试图在弹出窗口中打开链接,我已经写了这个静态html代码:
<a href="http://www.google.com" target="popup"
onclick="window.open('http://www.google.com','popup','width=600,height=600');
return false;"> Open Link in Popup
</a>
this code works, it open the link in popup. 此代码有效,它会在弹出窗口中打开链接。
But, if i want to add this code in javascript (with jquery) like this : 但是,如果我想在javascript中添加此代码(使用jquery),如下所示:
$('#services').append("<li><a href='http://www.google.com' target='popup' onclick='window.open('http://www.google.com','popup','width=600,height=600'); return false;'>Open Link in Popup</a></li>");
The link is opened in new tab and not in popup. 链接在新选项卡中打开,而不是在弹出窗口中打开。
Why ? 为什么呢
在jsfiddle上检查此示例 -问题在引号中
$('#services').append("<a href='http://www.google.com' target='popup' onclick=window.open('http://www.google.com','popup','width=600,height=600'); return false;>Open Link in Popup</a>");
You missed to escape the quotes in onClick
of your jQuery. 您错过了对jQuery
onClick
中的引号进行转义的操作。 Update the script as below. 如下更新脚本。
$('#services').append("<li><a href='http://www.google.com' onclick=\"window.open('http://www.google.com','popup','width=600,height=600'); return false;\">Open Link in Popup</a></li>");
Change your algorithm, instead of putting the javascript inside onclick, add a class or ID, with data-, so you can attach your jQuery event handler on that class/ID, and use the data to create new window. 更改您的算法,而不是将javascript放在onclick中,而是使用data-添加类或ID,以便您可以在该类/ ID上附加jQuery事件处理程序,然后使用数据创建新窗口。 Imagine the data as data-destination="http://www.destination".
将数据想象为data-destination =“ http://www.destination”。
That should fix your problem. 那应该解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.