2016-08-17 136 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>