如何使用 jquery 查找和替换 HTML 标签属性?
How to Find and Replace HTML Tags attribute by using jquery?
我想使用 Jquery.
查找和替换标记属性或更改内联 css
例如:<span style="text-decoration: underline;">
我想找到内联样式<span style="text-decoration: underline;">
并在页面加载时将其替换为<span class="line">
。
在 dom 准备就绪后,您可以定位所有跨度元素,例如
jQuery(function($){
$('span[style]').removeAttr('style')
})
如果要定位所有具有样式属性的元素
$('[style]').removeAttr('style')
在Jquery中很简单,只需使用以下内容,
$("span").removeAttr("style");
$("span").addClass("line");
试试这个:
$(document).ready(function(){
$('span[style]')
.removeAttr('style')
.addClass('line');
});
如果需要完全匹配下划线规则,可以添加约束条件。只需替换此:
$('span[style]')
有了这个:
$('span[style="text-decoration: underline;"]')
为了完整起见,这里有一个 JSFiddle。
$( document ).ready(function() {
$('span[style]').removeAttr('style').addClass("line")
})
这应该做
见Demo
Html
<p>My mother has <span style="text-decoration: underline;">blue</span> eyes and my father has <span style="text-decoration: underline;">dark green</span> eyes.</p><button onclick="btncheck();">Check</button>
jQuery
$(document).ready(function(){
$("button").click(function(){
$('span[style="text-decoration: underline;"]')
.removeAttr('style')
.addClass('line');
});
});
查看此演示:https://jsfiddle.net/yv5apdaj/
你可以这样做,根据需要更新css样式。
这样,您仍然可以使用其他样式并仅删除所需的 css 样式。
JQUERY
$(document).ready(function(){
$('span').each(function(){
if($(this).css('text-decoration') === 'underline'){
$(this).css({'text-decoration':''});
$(this).addClass('line');
}
});
});
HTML
<span style="text-decoration: underline;">Test Content</span><br /><br />
<span style="text-decoration: underline;">Test Content</span>
我想使用 Jquery.
查找和替换标记属性或更改内联 css例如:<span style="text-decoration: underline;">
我想找到内联样式<span style="text-decoration: underline;">
并在页面加载时将其替换为<span class="line">
。
在 dom 准备就绪后,您可以定位所有跨度元素,例如
jQuery(function($){
$('span[style]').removeAttr('style')
})
如果要定位所有具有样式属性的元素
$('[style]').removeAttr('style')
在Jquery中很简单,只需使用以下内容,
$("span").removeAttr("style");
$("span").addClass("line");
试试这个:
$(document).ready(function(){
$('span[style]')
.removeAttr('style')
.addClass('line');
});
如果需要完全匹配下划线规则,可以添加约束条件。只需替换此:
$('span[style]')
有了这个:
$('span[style="text-decoration: underline;"]')
为了完整起见,这里有一个 JSFiddle。
$( document ).ready(function() {
$('span[style]').removeAttr('style').addClass("line")
})
这应该做
见Demo
Html
<p>My mother has <span style="text-decoration: underline;">blue</span> eyes and my father has <span style="text-decoration: underline;">dark green</span> eyes.</p><button onclick="btncheck();">Check</button>
jQuery
$(document).ready(function(){
$("button").click(function(){
$('span[style="text-decoration: underline;"]')
.removeAttr('style')
.addClass('line');
});
});
查看此演示:https://jsfiddle.net/yv5apdaj/
你可以这样做,根据需要更新css样式。
这样,您仍然可以使用其他样式并仅删除所需的 css 样式。
JQUERY
$(document).ready(function(){
$('span').each(function(){
if($(this).css('text-decoration') === 'underline'){
$(this).css({'text-decoration':''});
$(this).addClass('line');
}
});
});
HTML
<span style="text-decoration: underline;">Test Content</span><br /><br />
<span style="text-decoration: underline;">Test Content</span>