我将 div 设为可编辑。当我尝试解析 div 的文本时,我需要执行以下正则表达式。
innerDOM = "<div style="cursor: text;">I had downloaded all the material from the Intern,<br>You will find</div><div style="cursor: text;"> </div><div style="cursor: text;">dfvdfvdvdfvdfvdvdvdf</div><div style="cursor: text;"> </div><div style="cursor: text;">dfvdfvdvdfvdfvdfvdfvd</div>"
innerDOM.replace(/<div style="cursor: text">/g, "<br>").replace(/<\/div>/g, "");
上面的正则表达式在 Firefox 和 Chrome 中效果很好。但在 IE 中不行。我应该做什么改变?
看到这个FIDDLE为了更好地理解...
DOM 操作就是其中之一jQuery是为.学习 jQuery 会让你在编写 DOM 遍历和修改方面走得更远。
$("div[style='cursor: text']").unwrap().prepend("<br>");
unwrap删除元素但保持子元素完好无损。 jQuery属性等于选择器用于选择全部divs
与光标样式属性。您可以实时运行它here.
您可以使其更加健壮,因为目前您不会选择具有或多或少空白或其他细微差异的 div。例如:<div style="cursor:text;">
与上面的选择器不匹配。您可以通过引入设置光标的 CSS 类来解决此缺点。在这种情况下<div style="cursor: text">
变成<div class='textCursor'>
您可以使用类选择器:$("div.textCursor")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)