2012-10-09 51 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>