我想从字符串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>
答案 0 :(得分:2)
更改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
答案 1 :(得分:0)
这很简单,请参阅:
var $html = $('<div />', {html: result.Html});
$html.find('div').removeAttr('ondragover ondragleave ondrop');
$('#divLayout').html($html.html());