2015-12-01 58 views
-2

我正在爲一個網站創建商店定位器,並且我得到了一個工具提示框,顯示我想要創建鏈接的電子郵件。這是HTML:Javascript .click()不起作用

<div class="gm_popup"> 
    <span class="address"> Address</span><br> 
    <span class="city"> City</span><br> 
    <span class="phone"> 123 456</span><br> 
    <span class="email"> [email protected]</span><br> 
</div> 

,這是JS:

jQuery(document).ready(function(){ 

    var old_focus_and_popup = focus_and_popup; 

    focus_and_popup = function (id){ 
    return_val = old_focus_and_popup (id); 

    setTimeout(function(){ 
     jQuery("#store_map .gm_popup .email").click(function(){ 
     var text = jQuery(this).text(); 
     document.location = "mailto:" + text.trim(); 
     }); 

     jQuery(".gm_popup .email").css('text-decoration','underline').css('color','#2aa8e0'); 
    }, 200); 
    return return_val ; 
    } 
}); 

任何想法,爲什麼這不會是工作?在添加了一些console.log之後,它看起來好像只有.click,但沒有進入函數。謝謝

+3

你爲什麼要動態地設置'的mailto:'功能?爲什麼不把它定義爲超鏈接呢? –

+4

爲什麼你在'setTimeout'中有點擊事件? –

+0

其使用shopify和它的創建方式是[電子郵件]。當你嘗試把HTML放在它周圍時,它會創建整個跨度而不是裏面的值 –

回答

1

您正在註冊點擊事件

只有手動完成任何點擊操作後纔會觸發。

如果你想獲得自動點擊然後再嘗試這樣的註冊click事件之後

jQuery("#store_map .gm_popup .email").click(); 

jQuery("#store_map .gm_popup .email").trigger("click"); 
+0

它不會進入點擊功能。我把一個console.log但它不顯示 –

+0

@AlexMurray你能提供小提琴嗎? –

0

那麼你必須在函數內部的所有事件的代碼,你永遠不會最終調用該功能。所以我做了一些修改,刪除了setTimeout,因爲這是不必要的。此外,還打了一個電話給這個功能。

<div id="store_map"> 
    <div class="gm_popup"> 
    <span class="address"> Address</span><br> 
    <span class="city"> City</span><br> 
    <span class="phone"> 123 456</span><br> 
    <span class="email"> [email protected]</span><br> 
</div> 

</div> 


jQuery(document).ready(function(){ 



    focus_and_popup = function (id){ 
    //return_val = old_focus_and_popup (id); 


     jQuery("#store_map .gm_popup .email").click(function(){ 
     var text = jQuery(this).text(); 
     console.log("HERE"); 
     document.location = "mailto:" + text.trim(); 
     }); 

     jQuery(".gm_popup .email").css('text-decoration','underline').css('color','#2aa8e0'); 

    return return_val ; 
    } 
    focus_and_popup(); 
    var old_focus_and_popup = focus_and_popup; 
}); 
+0

即時獲取此錯誤未捕獲ReferenceError:return_val未定義任何想法爲什麼? –

+0

對不起取消/ return_val權利 –

0

我給你寫了一個簡單的代碼片段。

jQuery(document).ready(function() { 
 

 
     jQuery(".gm_popup .email").click(function() { 
 
     alert('hello'); 
 
     var text = jQuery(this).text(); 
 
     document.location = "mailto:" + text.trim(); 
 
     }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="gm_popup"> 
 
    <span class="address"> Address</span> 
 
    <br> 
 
    <span class="city"> City</span> 
 
    <br> 
 
    <span class="phone"> 123 456</span> 
 
    <br> 
 
    <span class="email"> [email protected]</span> 
 
    <br> 
 
</div>

+0

由於某種原因它沒有進入點擊功能 –

+0

我不確定,但也許你正在創建focus_and_popup和old_focus_and_popup之間的無限循環。 – sfiore