简体   繁体   English

的HTML <img> jQuery的onclick函数

[英]HTML <img> onclick function to jquery

I have aa grid of images which has it's HTML generated with PHP. 我有一个图像网格,它是用PHP生成的HTML。 Each image has an onclick() function, which has a PHP variable in the arguments. 每个图像都有一个onclick()函数,该函数的参数中包含一个PHP变量。 On the page that the HTML is on, I have a Javascript function which uses the arguments in the onclick(arg) function. 在HTML所在的页面上,我有一个Javascript函数,该函数使用onclick(arg)函数中的参数。

I've started to use jquery and I want to be able change this onclick() function into my jquery functions, while using the PHP arguments from the PHP page. 我已经开始使用jquery,并且希望能够在使用PHP页面中的PHP参数的同时将此onclick()函数更改为jquery函数。

This is the code from the PHP file that generates the HTML, as you can see, there is an onclick function, which would have an argument that is a number: 这是来自生成HTML的PHP​​文件中的代码,如您所见,有一个onclick函数,该函数的参数为​​数字:

Echo "<img src= '$link[Image_Link] . /133x100' id='$row[Item_Name]' title='$row[Item_Name]' class='clickableImage' alt='$just_name' onClick='addValue(". $credit_value .")' border=0 style='position: relative; top: 0; left: 0;'/>";

This is my Javascript function on the HTML page, it just changes some text on the page: 这是我在HTML页面上的Javascript函数,它只是更改了页面上的一些文本:

function addValue(credits){
    var value = document.getElementById("value").innerHTML;
    value = parseInt(value, 10);
    var totalValue = value+credits;
    document.getElementById("value").innerHTML = totalValue;
}

I already have a jquery function set up for when these images are clicked, but I want to be able to use the $credit_value variable from the PHP file. 我已经为单击这些图像时设置了jquery函数,但是我希望能够使用PHP文件中的$credit_value变量。 This is the start of my jquery function: 这是我的jquery函数的开始:

$().ready(function () {
    $('img.clickableImage').click(function () {
        if ($(this).data('selected')) { ......

You can use data-* prefixed custom attribute, Set the value 您可以使用data-*前缀的自定义属性,设置值

echo "<img  data-creditvalue='. $credit_value .' ......../>";

Script , to fetch use $.fn.data() as you are already using $(this).data('selected') 脚本 ,使用$.fn.data()进行获取,因为您已经在使用$(this).data('selected')

var creditvalue = $(this).data("creditvalue")

In your echo statement, put a custom data-* attribute like 在您的echo语句中,放置一个自定义data-*属性,例如

echo "<img data-credit='". $credit_value ."' src= '$link[Image_Link] . /133x100' id='$row[Item_Name]' title='$row[Item_Name]' class='clickableImage' alt='$just_name' border=0 style='position: relative; top: 0; left: 0;'/>";

and then in your click handler access it like 然后在您的点击处理程序中进行访问

$('img.clickableImage').click(function () {
        var credit = $(this).attr('data-credit');
});

Use Event delegation 使用事件委托

 $(document).ready(function () {
    $(document).on('click', 'img.clickableImage', function () {
            if ($(this).data('selected')) {
               // here your code
            }
    });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM