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='PearlSquirrel'/>
                </button>
                <button type='button' class='button2' onclick='downvote($id8)'>
                    <img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
                </button>
            </div>

当我点击 onclick 进行投票时,代码是这样的:

function upvote(box){
$(this).siblings('.upvoteimage').load('upvote.php?box=' + box);
}

我已经尝试了一切方法来让它发挥作用。获取功能正在运行,但我看不到“.upvoteimage”中的任何变化。有谁知道如何解决这个问题。帮助将不胜感激。谢谢。

最佳答案

尝试这种方式:为“button”元素设置一个“upvote”类:

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

并使用这个 JS 代码:

$(".upvote").on("click",function(){
    var box=$(this).attr("id");
    $(this).find('.upvoteimage').attr("src",'upvote.php?box=' + box);
    return false;
}​);​

关于javascript - 使用 Jquery 从页面上的许多重复类中获取特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242515/

相关文章:

javascript - 有没有办法删除 window.open 事件?

javascript - 在 jquery ajax 中将函数名称作为字符串传递

javascript - val().replace in 文本区域

javascript - 2 个数组之间的匹配值必须将选项 HTML 设置为禁用

javascript - 丰富的 Dojo 应用程序的服务器框架?

javascript - 覆盖图像覆盖自定义 Google map 网络中的标记

页面加载时的 JavaScript 按钮 "Selected"

javascript - 使用 javascript/jquery 更改 url 并使用新 url 重新加载页面

jquery - Rails Ajax 无法验证 CSRF token 的真实性

javascript - 测试后连接未关闭?