我正在尝试根据元素的背景属性从页面中删除元素。 就像是:
if ( $('td').attr('background','backgroundimageurl')){this.remove();}但这不起作用有什么建议吗?
最佳答案
您还可以使用过滤器根据您的操作过滤结果集:
$( 'td' ).filter( function(){
// return true of it matches, thus, keeping it in the object
return $(this).css( 'backgroundImage' ) === 'someUrlOrWhatever';
}).remove();
根据 roXon 的要求进行演示:http://jsfiddle.net/danheberden/9rTZj/
但是,最好像
这样进行检查return /someDomain\.com\/path\/to\/whatever/.test( $( this ).css( 'backgroundImage' ) );
在过滤函数中。不同的浏览器会为 css 规则返回不同的格式,因为 roXon 指出 ===
方法在 FF 中不起作用,因为返回的字符串将是 url("thePath") 而不是 url(thePath ) 就像在 webkit 中一样。因此,仅测试 url 值将是最灵活的。
关于javascript - 根据背景图像删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451122/