removeAttr在字符串html
removeAttr inside string html
我想从字符串html中删除attr。我有这个代码
var htm = result.Html;
$(htm, 'div').each(function () {
$(this).removeAttr('ondragover');
$(this).removeAttr('ondragleave');
$(this).removeAttr('ondrop');
});
$('#divLayout').html(htm);
,但问题是字符串保持原来的样子notice: result.Html equal:
<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());
演示:小提琴
这很简单,参见:
var $html = $('<div />', {html: result.Html});
$html.find('div').removeAttr('ondragover ondragleave ondrop');
$('#divLayout').html($html.html());
实际操作:http://jsfiddle.net/caio/JWaLc/
相关文章:
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- HTML字符串作为上下文
- 正在分析HTML字符串并从中进行选择
- 将GET请求(HTML字符串)转换为完整的DOM对象
- 在树枝模板上从数据库中解压缩HTML字符串
- 如何使用Razor显示模板将字符串转换为html字符串
- 如何在不打断标记的情况下突出显示html字符串中的文本
- 使用val()在jquery循环中设置html字符串的val
- 搜索并用jQuery替换整个HTML字符串
- 替换 html 字符串中的某些“<”和“>”
- Javascript-将包含变量的html字符串作为参数传递
- 如何用JavaScript替换HTML字符串
- 如何在我的模板(Angular2/TypeScript)中将HTML字符串转换为HTML
- 在HTML字符串的某个段落后插入HTML
- 使用 javascript 压缩包含嵌入图像的 HTML 字符串
- 需要使用Java编码/转义HTML字符串,并在JavaScript中解码HTML字符串
- 在不更改 HTML 的情况下操作 HTML 字符串的内容
- 使用JavaScript从HTML字符串中提取文本
- regex在JavaScript中拆分一个html字符串
- 创建HTML对象的Javascript与创建HTML字符串的比较