Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Jquery从页面上的许多重复类中获取特定元素_Javascript_Jquery - Fatal编程技术网

Javascript 使用Jquery从页面上的许多重复类中获取特定元素

Javascript 使用Jquery从页面上的许多重复类中获取特定元素,javascript,jquery,Javascript,Jquery,我有一段重复的代码 <div class='commentfloatleft'> <button type='button' class='button2' onclick='upvote($id8)'> <img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirre

我有一段重复的代码

<div class='commentfloatleft'>
                <button type='button' class='button2' onclick='upvote($id8)'>
                    <img src='images/likebutton.png'    class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/>
                </button>
                <button type='button' class='button2' onclick='downvote($id8)'>
                    <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
                </button>
            </div>

我已经尽了一切努力来让它工作。get函数正在工作,但我看不到“.upvoteimage”中有任何更改。有人知道如何解决这个问题吗。非常感谢您的帮助。谢谢。

你的代码上写的是兄弟姐妹,但upvoteimage不是兄弟姐妹,而是孩子。尝试改用find/children


而$(this)可能不存在于函数中。也许您应该将onclick事件移到脚本块。jQuery有一些很好的特性,可以不使用内联代码。

您的代码说是兄弟,但upvoteimage不是兄弟,而是孩子。尝试改用find/children


而$(this)可能不存在于函数中。也许您应该将onclick事件移到脚本块。jQuery有一些很好的特性,可以不使用内联代码。

您正在引用按钮的同级,并对upvoteimage类的同级进行筛选

您要做的是引用兄弟姐妹,然后选择upvoteimage类的子级:

$(this).siblings('.button2').children('.upvoteimage').load('upvote.php?box=' + box);

您正在引用按钮的同级,并筛选upvoteimage类的同级

您要做的是引用兄弟姐妹,然后选择upvoteimage类的子级:

$(this).siblings('.button2').children('.upvoteimage').load('upvote.php?box=' + box);
试试这个

HTML

  <div class='commentfloatleft'>
                    <button type='button' class='button2' onclick='upvote(this,\x27$id8\x27)'>
                        <img src='images/likebutton.png'    class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/>
                    </button>
                    <button type='button' class='button2' onclick='downvote($id8)'>
                        <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
                    </button>
                </div>
试试这个

HTML

  <div class='commentfloatleft'>
                    <button type='button' class='button2' onclick='upvote(this,\x27$id8\x27)'>
                        <img src='images/likebutton.png'    class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/>
                    </button>
                    <button type='button' class='button2' onclick='downvote($id8)'>
                        <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
                    </button>
                </div>

这是一个孩子,不是兄弟姐妹这是真的,这不是真的

你可以试试

$(this).siblings('.button2' > '.upvoteimage') 

我想有些类似的事情会起作用

这是一个孩子而不是兄弟姐妹,这是不可能发生的

你可以试试

$(this).siblings('.button2' > '.upvoteimage') 
我认为一些类似的方法可以工作

您应该使用它来绑定事件,而不是内联操作

在您的
upvote
函数
中,此
是窗口对象,而不是单击的按钮

upvote类的img是按钮的子级,而不是兄弟级

您不能将任何内容加载到img标记中,它是一个空标记,您应该使用它来绑定事件,而不是内联操作

在您的
upvote
函数
中,此
是窗口对象,而不是单击的按钮

upvote类的img是按钮的子级,而不是兄弟级

您不能将任何内容加载到img标记中,它是一个空标记

尝试以下方法:将类“upvote”设置为您的“button”元素:

尝试以下方法:将类“upvote”设置为“button”元素:


要激活按钮,它们需要被
不真实所包围,这只是为了激活表单。他已经有了(内联,不是最佳实践,但它是有效的)在
upvote($id8)中使用的
$id8
是什么类型的变量
?这是注释的特定id。要激活按钮,它们需要被
不真实包围,这只是为了激活表单。他已经有了(内联,不是最佳实践,但它有效)在
向上投票($id8)中使用的
$id8
是什么类型的变量
?这是注释的特定id。Ashirvad Singh回答了完整的文件包(只是还没有全部解释)Ashirvad Singh回答了完整的文件包(只是还没有全部解释)
$(".upvote").on("click",function(){
    var box=$(this).attr("id");
    $(this).find('.upvoteimage').attr("src",'upvote.php?box=' + box);
    return false;
}​);​