2016-08-17 130 views
2

我有一个jQuery通知。当我点击通知时,它应该进入预期的页面。如何在jQuery中创建链接?

在PHP中,我们可以通过以下操作来实现。

$link = 22; 
echo "<a href=\"page2.php?id=link\">Click to read more</a>"; 

如何在JQuery中实现这样的功能?我有弹出通知和链接变量准备好。

var link = "home.php?destination=22"; 

回答

4

您可以使用jQuery

var link = "home.php?destination=22"; 
 

 
//Create anchor element 
 
var anchor = $('<a />', { 
 
    "href": link, 
 
    "text": "Click to read more" 
 
}) 
 

 
//Append the element 
 
$('#dialog').append(anchor).dialog();
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
 

 
<div id="dialog" title="Basic dialog"> 
 
    
 
</div> 
 

+0

我该如何把链接放在线上,让我们说alert()?当我把锚定在警报中时,它变成了对象。示例警报(锚点); – tapaljor

+0

请帮我把链接放在alert(); – tapaljor

+0

@tapaljor,我已经更新了代码 – Satpal

0

创建HTML元素,你可以在Java脚本中使用window.location.href实现它。

<button onclick="myFunction()">Click me to read more</button> 
<script> 
function myFunction() { 
    window.location.href = 'home.php?destination=22'; 
} 
</script>