gpt4 book ai didi

javascript - html字符串中的removeAttr

转载 作者:行者123 更新时间:2023-12-02 17:27:18 25 4
gpt4 key购买 nike

我想从字符串 html 中删除 attr。我有这个代码

 var htm = result.Html;
$(htm, 'div').each(function () {
$(this).removeAttr('ondragover');
$(this).removeAttr('ondragleave');
$(this).removeAttr('ondrop');
});
$('#divLayout').html(htm);

但是问题是字符串保持原来的样子注意:result.Html 等于:

    <div class="updiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
<div id="fchoise" class="detdiv containertwo">
<div id="df4a6783-beb2-2cdf-0b1d-611c4d7b195f" class="div1" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
</div>
<div class="updiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
<div id="ghchoise" class="containertwo detdiv">
<div id="932e29b5-b6fe-97f5-d3dc-21768291ec90" class="lefts" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
<div id="cfac8011-0e4e-3eba-aaaa-ac36b58b1512" class="rights" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
</div>
<div class="downdiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
<div id="thchoise" class="containertwo detdiv">
<div id="3b8b92b3-45f9-54b2-b01a-60b60f65f175" class="lefts" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
<div id="c73e2dc9-9980-774b-5d50-c35336d8201d" class="rights" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>
</div>
<div class="downdiv containertwo" ondrop="drop(event)" ondragleave="dragleave(event)" ondragover="allowDrop(event)"></div>

最佳答案

更改 jQuery 对象不会修改原始字符串,因此您需要

var htm = result.Html;

var $tmp = $('<div />', {
html: htm
});
$tmp.find('div[ondragover]').removeAttr('ondragover');
$tmp.find('div[ondragleave]').removeAttr('ondragleave');
$tmp.find('div[ondrop]').removeAttr('ondrop');

$('#divLayout').html($tmp.html());

演示:Fiddle

关于javascript - html字符串中的removeAttr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23420033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com