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 use execCommand('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
Add a ment  | 

2 Answers 2

Reset to default 15

Well, 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