簡體   English   中英

jQuery .click函數轉換為javascript

[英]jQuery .click function converted to javascript

當用戶單擊我們網站外部的任何鏈接時,我編寫了此腳本來彈出確認對話框。 它在我們的Internet站點上按預期方式運行,但是當我嘗試在FAQ門戶上運行此腳本時,它什么也沒做。 我相信它阻止了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>

請參閱此SO問題,以獲取有關不干擾Javascript的更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM