我需要像tr按鈕中的按鈕來點擊時執行js/jquery事情。如何創建激活jQuery/js功能的圖像按鈕
奇怪的是,我很難找到關於如何從內聯html/image按鈕執行jquery/js的很好的知識。
這是我腦子裏想的:
首先創建一個自定義的功能類似按鈕
jQuery.fn.likebutn = function(theword, wordnumber) {
//check if button is lighted up and if it is break
//change button to lighted up
jQuery(this).attr("src", "http://i46.tinypic.com/n18g3q.png");
//add word to a div element
jQuery('#cntrldiv0').append("<p>theword<p>");
//ajax like.php file with the word and username/id
jQuery.post('like.php', {theword:theword, userid: userid});
};
然後動態插入函數調用和它的參數(如在PHP正在創建的表)在圖像按鈕元件(在HTML,所以內置)
likebutn({theword: ${'row' . $wordnumber}, wordnumber:$wordnumber});
其輸出
likebutn(theword:有孔,wordnumber:1)
例如。
那麼如何從HTML中喚起這樣的功能呢?
而在旁註中,爲什麼在瀏覽網頁(而不是我所建議的)時看到的jquery/js圖像按鈕中有太多的事件監聽,並不比在html中引用內聯慢?
這裏的結構是什麼樣子:http://jsfiddle.net/Bc64R/
你知道嗎,你只需要以其長格式編寫'jQuery'一次?通過將代碼封裝在'(function($){....})(jQuery);'中,無論是否使用了noConflict,都可以使用'$'。 – ThiefMaster
@ThiefMaster我沒有= O我的意思是noConflict,那麼noConflict有什麼意義呢?無論如何,使用jQuery而不是$是我的一個個人選擇,因爲我正在處理大量的php。但是,也許當我準備好發佈代碼時,我只需在所有文檔中用$替換所有的jQuery。 – Happy
嗯,這是更多的爲你寫,更難以閱讀幾乎每個習慣'''的人。 'noConflict'的意思是不要使用'$'作爲全局變量。在匿名函數'$'的關閉內部沒有問題 - 它不會干擾使用全局'$'的其他任何操作。 – ThiefMaster