2012-10-09 53 views
2

如何向超鏈接添加Javascript提醒? 我想在有人點擊鏈接時彈出警報。 這是現在的劇本 -將javascript添加到超鏈接

<a href="<?php echo ($link1);?>" onclick="popitup();" data-role="button" data-theme="b" data-inline="true" > Send </a> 

,我想將它添加到鏈接

<script type="text/javascript">alert('Text.');</script> 

回答

1

試試這個

<a href="new.html" onclick="alert('This is going to link')"> link </a>

+0

Darin的解決方案可能是更好儘管編寫javascript in-line是不好的做法。 – lifetimes

2

應定義popitup()功能,你打電話給你的onclick屬性:

<script type="text/javascript"> 
    function popitup() { 
     alert('Text.'); 
    } 
</script> 

此外,如果你想避免在特定條件下重定向你可以從該處理程序返回false鏈接:

<a href="<?php echo ($link1);?>" onclick="return popitup();" data-role="button" data-theme="b" data-inline="true"> Send </a> 

然後:

<script type="text/javascript"> 
    function popitup() { 
     if (confirm('Are you sure you want to do that?')) { 
      return true; 
     } 
     return false; 
    } 
</script> 
0

您可以添加JavaScript語句到href屬性通過把javascript:在聲明之前,所以它會是這樣的:

<a href="javascript: alert('Text.');" ></a> 
+0

再次,不是最佳實踐! – Rayner

2

如果警報應該做某種邏輯,你可以使用恢復功能這樣

<a href="newlink.html" onClick="return confirm('do you really wanna go to this link?')" >the link</a> 

或者只是添加警報W/O任何邏輯這樣

<a href="newlink.html" onClick="alert('you are going to the link! beware!')" >the link</a>