문제

I have the following code:

<span class='myClass'>My Text</span><button class='myBtn'>OK</button>
<span class='myClass'>My Text</span><button class='myBtn'>OK</button>
<span class='myClass'>My Text</span><button class='myBtn'>OK</button>

I'm looking to click on any of myClass and remove it's contents PLUS remove button right after that. I don't want to remove others - just the one I click on.

Thanks for any help.

도움이 되었습니까?

해결책

If you are using jQuery:

$('.myClass').click(function() {
    $(this).next().remove();
    $(this).remove();
});

If plain javascript:

var spans = document.getElementsByClassName('myClass');
for(var i = 0; i < spans.length; i++) {
    spans[i].onclick = function() {
        this.parentNode.removeChild(this.nextSibling);
        this.parentNode.removeChild(this);
    };
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top