开发者

Access Parent Element Within href

开发者 https://www.devze.com 2023-02-11 23:13 出处:网络
So... this is a simple question with a simple answer, but somehow my RTFMing isn\'t proving any results.

So... this is a simple question with a simple answer, but somehow my RTFMing isn't proving any results.

I have a link within a div, like so:

<div> <a href=''>Close&开发者_开发技巧lt;/a>

I'd like to close that div with that link, and I've been trying the following code:

<div> <a href="javascript:this.parentNode.style.display='none'">Close</a>

However, it still hasn't worked... any suggestions are greatly appreciated.


Change it to this:

<div> <a href="#" onclick="this.parentNode.style.display='none'">Close</a>

The reason is that when using href="javascript:..., this doesn't refer to the element that received the event.

You need to be in an event handler like onclick for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消