Home  >  Article  >  Web Front-end  >  Implementation ideas for this object passed through href and onclick in A tag_javascript skills

Implementation ideas for this object passed through href and onclick in A tag_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:36:111163browse

In the backend management of the blog, it is allowed to add an address to a category, but it is not easy to add an onclick event. I wanted to pass the current object to a function, so I wrote the URL as "Javascript:shoControlSidebar(this)", but it turned out that this was not feasible. The parameter passed was an object, but no other information was obtained. What I want to get is innerText, and this this does not point to the A tag where it is located.

This is the difference between and .

When using onclick="shoControlSidebar(this)", the interpreter will wrap it with an anonymous function, which becomes:

Copy code The code is as follows:

a.onclick = function anonymous()
{
shoControlSidebar(this);
}

This this refers to the object a. When using href, since it is an address, this this has nowhere to refer to.
Copy code The code is as follows:

Copy code The code is as follows:

Want to get the innerHTML in A
If href="test(this);" not only cannot the value be obtained, but the program will exit, and the href is directed incorrectly.

Copy code The code is as follows:

function test(obj){
alert(obj);
//js
alert(obj.innerHTML);
//jquery
alert($(obj).html());
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn