jQuery .click 函数转换为 javascript

jQuery .click function converted to javascript

我编写此脚本是为了在用户单击我们网站外部的任何 link 时弹出确认对话框。它在我们的 internet site, however when I try to run this script on our FAQ portal 上的表现符合预期,它什么也没做。我相信它正在阻止 运行 中的 jQuery。将此脚本转换为 javascript 有何建议?

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">

    $(function(){
        $('a').click(function(){

        if ((this.href.toLowerCase().indexOf("myfloridalicense.custhelp") > -1) || 
            (this.href.toLowerCase().indexOf("myfloridalicense") > -1) ||
            (this.href.toLowerCase().indexOf("javascript") > -1) ||
            (this.href.toLowerCase().indexOf("dbprftp") > -1) ||
            (this.href.toLowerCase().indexOf("interredesignalpha") > -1) ||
            (this.href.toLowerCase().indexOf("bpr") > -1))
        {

            //Throw away
        } 
        else {
            if (window.confirm('NOTICE: By accessing this link, you will be leaving the DBPR website. DBPR is not responsible for the content of the Internet website you are entering. DBPR neither warrants nor makes any representations nor endorsements as to the accuracy, quality, content or completeness of the information, text, images, graphics, hyperlinks, and other items contained on the Internet website you are entering. DBPR is not responsible or liable for any viruses or contaminations of your hardware, software, peripherals or property, resulting from use of the Internet websites linked to or from the DBPR Internet website. Do you want to proceed?'))
            {
                // They clicked Yes
            }
            else
            {
                // They clicked no
                return false;
            }
        }
        });
    });
</script>

在此先感谢您的帮助!

function test(){
  event.preventDefault(); 
  var path = $('a').prop('href');
  if ((path.toLowerCase().indexOf("myfloridalicense.custhelp") > -1) || 
        (path.toLowerCase().indexOf("myfloridalicense") > -1) ||
        (path.toLowerCase().indexOf("javascript") > -1) ||
        (path.toLowerCase().indexOf("dbprftp") > -1) ||
        (path.toLowerCase().indexOf("interredesignalpha") > -1) ||
        (path.toLowerCase().indexOf("bpr") > -1))
    {

        //Throw away
    } 
        else {
            if (window.confirm('NOTICE: By accessing this link, you will be leaving the DBPR website. DBPR is not responsible for the content of the Internet website you are entering. DBPR neither warrants nor makes any representations nor endorsements as to the accuracy, quality, content or completeness of the information, text, images, graphics, hyperlinks, and other items contained on the Internet website you are entering. DBPR is not responsible or liable for any viruses or contaminations of your hardware, software, peripherals or property, resulting from use of the Internet websites linked to or from the DBPR Internet website. Do you want to proceed?'))
            {
                // They clicked Yes
            }
            else
            {
                // They clicked no
                return false;
            }
        }


}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="www.google.com" onClick="javascript:test(); return false;"> Link </a>

我的第一个猜测是您的门户为所有锚标记附加了一个侦听器,以防止离开门户页面。很有可能,jQuery没有被屏蔽,点击锚标签被屏蔽了。

您可以通过绑定到锚标记(或特定标记)上的 mouseup 事件来验证这一点。有可能门户也在取消这些事件,但很多人只想着绑定点击。

如果您对页面有任何控制权,请尝试将点击侦听器添加到锚标记以外的其他内容,比如特定的 div 或列表项。这将回答 jQuery 是否处于活动状态。知道该问题的答案后,您就可以决定下一步的行动了。

与 Keerthi 的回答相同,但使用了不显眼的 Javascript:

function test(){
 if ((this.href.toLowerCase().indexOf("myfloridalicense.custhelp") > -1) || 
            (this.href.toLowerCase().indexOf("myfloridalicense") > -1) ||
            (this.href.toLowerCase().indexOf("javascript") > -1) ||
            (this.href.toLowerCase().indexOf("dbprftp") > -1) ||
            (this.href.toLowerCase().indexOf("interredesignalpha") > -1) ||
            (this.href.toLowerCase().indexOf("bpr") > -1))
        {

            //Throw away
        } 
        else {
            if (window.confirm('NOTICE: By accessing this link, you will be leaving the DBPR website. DBPR is not responsible for the content of the Internet website you are entering. DBPR neither warrants nor makes any representations nor endorsements as to the accuracy, quality, content or completeness of the information, text, images, graphics, hyperlinks, and other items contained on the Internet website you are entering. DBPR is not responsible or liable for any viruses or contaminations of your hardware, software, peripherals or property, resulting from use of the Internet websites linked to or from the DBPR Internet website. Do you want to proceed?'))
            {
                // They clicked Yes
            }
            else
            {
                // They clicked no
                return false;
            }
        }
    }

document.getElementById('mySuperImportantLink').addEventListener('click', test, false);

HTML:

<a href="javascript:void(0)" id="mySuperImportantLink"> Link </a>

参见this SO question for more details on unobtrusive Javascript