jQuery 查找和替换多个属性

jquery find and replace for multiple attributes

本文关键字:属性 替换 查找 jQuery      更新时间:2023-09-26

抱歉,我刚刚真正开始接触javascript,我无法确定语法。

这是我的脚本:

jQuery(document).ready(function ($) {
    $('div[align="right"][style="margin-right:15px;"]').each(function () {
        $(this).removeAttr('align')
        $(this).removeAttr('style');
        $(this).addClass('homepagecontent2');
    });
});

基本上,我只想找到每个带有align="right"style="margin-right:15px;"的div,然后删除alignstyle并添加类。

当我只寻找align="right"时,它工作正常,但是当我将第二个元素添加到等式中时,它会中断。

最好按CSS样式过滤元素:

$("div[align='right']").filter(function() {
    return $(this).css("margin-right") === "15px";
}).removeAttr("align style").addClass("homepagecontent2");