javascript - jquery:单击以开头的每个 id 元素

标签 javascript jquery jquery-selectors

我试图找到所有以字符串“matchItem_”开头的id,并为每个id定义一个点击事件。 onclick 我希望执行带有 URL 参数的脚本并更改此 id 元素中的图像。不幸的是我的语法不正确,我也尝试过 .each.function 但我没有得到它。

$('[id^="matchItem_"]').each {
    
    $(this).click(){
     //...execute my script.php?urlparam=xx....;
     $(this).find('img').attr('src','/admin/images/ok.png');
    }

}

最佳答案

您不需要each:

$('[id^="matchItem_"]').click(function() {
   // do something
   $(this).find('img').attr('src','/admin/images/ok.png');
});

关于javascript - jquery:单击以开头的每个 id 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6985136/

相关文章:

Javascript 和 MySQL

javascript - 我如何知道 XHR 请求出了什么问题?

javascript - 如何从 Javascript 类中通过 Ajax 发送对象?

php - 通过 ajax 向 Controller 发送请求,从命名空间中删除斜杠?

jquery - CSS - 添加类到 3n+4 'visible' 列表项

jQuery 不识别动态添加到页面的类

javascript - 在 react 导入 3rd 方 jQuery,CSS,Java 脚本到 index.html 比使用 npm 或 yarn 更好

javascript - 在滚动上滑动 Div

javascript - 错误 403 : jquery bootstrap and css

jQuery:选择选中的复选框