如何创建激活jQuery/js函数的图像按钮

How to create image buttons that activate jQuery/js function

本文关键字:js 函数 按钮 图像 jQuery 激活 何创建 创建      更新时间:2023-09-26

我需要在tr元素中具有类似的按钮,以便在单击时执行js/jQuery操作。

奇怪的是,我很难找到有关如何从内联html/图像按钮执行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});

哪些输出

喜欢的布滕(单词:有孔,字数:1)

例如。

那么我如何从 html 中唤起这样的函数呢?

附带说明一下,为什么我在上网时看到的jQuery/js图像按钮中有这么多事件监听(而不是我的建议),这不是比在html中内联调用慢吗?

以下是构造的外观:http://jsfiddle.net/Bc64R/

除非你有一个名为foraminated的变量,否则这是不正确的:

likebutn(theword:foraminated, wordnumber:1)

应该是:

likebutn(theword:"foraminated", wordnumber:1)

从 html:

<...onclick>