2015-09-30 44 views
1

下面是我的腳本調用函數的HREF

$('<a class="page_link" href="javascript:'+getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + i)+'" longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link'); 

像上面我試圖呼籲href屬性的功能。但是在href上顯示undefined。

這樣做的正確方法是什麼?

+0

爲什麼你不綁定點擊事件?你的預期結果是什麼? –

+0

實際上是在for循環中創建一個錨點元素,這樣我就可以使用頁面= 1,page = 2調用getResults函數,就像使用i值的那個函數一樣 – vamsi

回答

1

我。會推薦使用jQuery創建HTML並使用它綁定事件。參考jQuery(html, attributes)

$('<a />', { 
    'class' : 'page_link', 
    'longdesc' : 0, 
    'text' : i 
}).css({ 
    'display' : 'inline-block' 
}).on('click', function(){ 
    getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + $(this).text()); 
}) 
.insertBefore('.next_link'); 
0

你接近,你只需要更新您的href到: -

$('<a class="page_link" href="javascript:getResults("http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + i + '") longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link'); 

你混合了「&」

0

你可以試試下面的代碼:

$('<a class="page_link" href="javascript:getResults(\'http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page='+ i+'\');" longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link'); 
0

這是一個工作demo

HTML

<div id="hello"></div> 

JS

$('<a/>', { 
    href: '#', 
    class: 'page_link', 
    html: 0, 
    click: function() { alert("Hi"); } 
}).appendTo($('#hello')); 

希望這會幫助你。

注意:html是您想要顯示超鏈接的文本。

1

而不是每添加元素結合的情況下,你可以創建容器上的一個事件,或身體:

ready功能:

$('body').on('click', 'a.page_link', function (e) { 
    e.preventDefault(); 
    getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + $(this).text()); 
}); 

每當你想插入一個新的元素:

$('<a class="page_link" href="#" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link');