[英]How can i find and replace style in specific HTML tag with regex in javascript?
正如我在標題中提到的,如何在給定的 HTML 標簽中更改 style 屬性的內容?
例如:
<table class="MsoTableLightShadingAccent1" border="1" cellspacing="0" cellpadding="0" style="margin-left: 45.9pt; border: none;">
我有很多這樣的html代碼。 我在 js 中寫了一個這樣的 RegEx:
str = str.replace(/(<(?:table)[^<]*)(style="[^"]*")([^>]*>)/gi,'<table style="font-size:medium;">');
這可以更改表格標簽,但實際上我只想刪除“左邊距”。 我怎么能用js做到這一點?
編輯:我使用summernote。 當用戶粘貼文本時,它會觸發 onPaste 回調並清理臟標簽和屬性。
您不應該使用正則表達式解析 HTML,請參閱這個著名的答案。
JavaScript 完全有能力做你想做的事,就像這樣:
<div id="someID" style="height: 50px; width: 50px; background-color: red; margin-left: 50px;"></div>
<button type="button" onclick="document.getElementById('someID').style.marginLeft = 0;">Click Me!</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.