2011-12-21 41 views
0

我需要使用jQuery生成將近80個超鏈接。我做到了,一切都很好。現在我的客戶想讓我在超鏈接上添加點擊事件?我怎樣才能做到這一點?如何將內聯onclick事件添加到使用jquery的超鏈接?

我現在的鏈接是:

<a href="#" class="removed purchase-btn">Long Sleeve Breton Top</a> 

,但我需要補充一點:

$('<a onclick="site.exec('page', 'openInternal', {'url': 'http://www.site.in/en-GB/Products/quickshop.aspx?qsi=AM154YEL&amp;});" href="#">link'+i+'</a>').appendTo('#hello'); 
    } 

的是,我用這個功能,但沒有使用:

$(function(){ 

    for(i=0;i<80;i++){ 
    $('<a onclick="site.exec('page', 'openInternal', {'url': 'http://www.site.in/en-GB/Products/quickshop.aspx?qsi=AM154YEL&amp;});" href="#">link'+i+'</a>').appendTo('#hello'); 
    } 

}) 

但它不起作用。任何想法,使這項工作?

這個環節我需要生成並投入應用。所以直接從jquery的點擊事件我不能使用。嚴格我需要將此代碼放在內聯..

+1

用[。點擊()](http://api.jquery.com/click/) – Blazemonger 2011-12-21 15:12:42

回答

0

使用的事件處理程序,而不是內嵌代碼

$(function(){ 
    var parent = $("#hello"); //Get the element that will hold all the links. 

    for(var i=0;i<80;i++){ 
     parent.append("<a href='http://www.site.in/en-GB/Products/quickshop.aspx?qsi=AM154YEL&amp;'>link"+i+"</a>"); 
     //Append a link with the URL in its href attribute 
    } 

    parent.on("click", "a", function(e) { //Select links under the #hello element and provide a click handler 
     site.exec('page', 'openInternal', { 
      'url': this.href //Use the href property of the link 
     }); 
     e.preventDefault(); //Don't follow the href link 
    }); 
} 
0

也許這將幫助:

$("a.removed").live("click", function(){ 
    //do something 
}); 
+0

'.live()'被棄用的jQuery 1.7的,並且是甚至在此之前就皺起了眉頭。我認爲這種綁定比'.click()'更好,但'.on()'應該是1.7之前使用的函數(或'.delegate()')。 – 2011-12-21 15:26:50

0

如果你的鏈接被包裹在另一個DIV然後:

$("#divid a").each().click(function(){ 

    ...... 

}); 

$("a").each().click(function(){ 

    ...... 

}); 

根據您的要求可能會解決您的問題,進一步提高了它。

編輯:在引號包裹選擇

+0

這裏不要使用'.each()'。 – andlrc 2011-12-21 15:19:29

+0

和remomber引號編輯:'$( 「A」)點擊(函數(){...' – andlrc 2011-12-21 15:20:33

+0

@AndreasAL對不起,忘了有急事......感謝名單BTW :) – xmaestro 2011-12-21 15:23:46

相關問題