admin管理员组文章数量:1391925
What's the best way to remove the strong
element during the onclick event?
<html xmlns="">
<head>
<title>Untitled Page</title>
</head>
<body>
<span onclick="testfx();"><strong>Test without styles</strong></span>
</body>
</html>
<script type="text/javascript">
function testfx() { alert('test'); }
</script>
What's the best way to remove the strong
element during the onclick event?
<html xmlns="http://www.w3/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<span onclick="testfx();"><strong>Test without styles</strong></span>
</body>
</html>
<script type="text/javascript">
function testfx() { alert('test'); }
</script>
Share
Improve this question
edited Jan 28, 2017 at 3:03
Ry-♦
225k56 gold badges493 silver badges499 bronze badges
asked Jul 2, 2013 at 15:04
RodRod
15.5k35 gold badges134 silver badges264 bronze badges
5
-
1
If you want to remove the tags only( and preserve the text), you could make a selection of the
span
and then useexecCommand('removeFormat')
. – Teemu Commented Jul 2, 2013 at 15:09 -
Please provide a more detailed explanation of your problem. If you want to remove the
strong
element while preserving the content, have a look at my answer here. – Felix Kling Commented Jul 2, 2013 at 15:15 - I think you should alter the title for more generic context so it can be more easy for people find this.. something like "Remove child element but keep it's content".. something like that. Just a tip. – DontVoteMeDown Commented Jul 2, 2013 at 15:20
- I would like to preserve the content – Rod Commented Jul 2, 2013 at 15:21
-
In that case, have a look at the
unwrap
function I linked to in my ment. – Felix Kling Commented Jul 2, 2013 at 21:43
2 Answers
Reset to default 15Well, here it is: http://tinker.io/1f282/1
el.addEventListener('click', function () {
var toRemove = this.getElementsByTagName('strong')[0];
if (!toRemove) {
return;
}
while (toRemove.firstChild) {
el.appendChild(toRemove.firstChild);
}
});
(where el
is your span). Note one very important thing: You should not mix javascript in your html. It's bad in all kinds of ways, you can just google "separation of concerns" and "unobtrusive javascript" to see why.
You should be handling (javascript) events in a script file and not in the HTML. It will help you with,
- Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
- Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
- Progressive enhancement to support user agents that may not support advanced JavaScript functionality
You could try this,
var x = document.getElementById("test");
var y = document.getElementById("testChild");
x.addEventListener('click', function () {
this.removeChild(y);
});
Test Link
EDIT
If you just want to remove the "strong" effect do this,
var x = document.getElementById("test");
var y = document.getElementById("testChild").innerHTML;
x.addEventListener('click', function () {
this.innerHTML = y;
});
Test Link
本文标签: javascriptRemove child element but keep its contentStack Overflow
版权声明:本文标题:javascript - Remove child element but keep its content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744711980a2621182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论