This time I will show you how to use the href attribute of the a tag and the onclick event, and how to use the href attribute of the a tag and the onclick eventNotesWhat are they? Here are actual cases. Let’s take a look. The
a tag is mainly used to implement page jumps, which can be achieved through the href attribute or in the onclick event.
<a onclick="window.location.href='m.sbmmt.com'" href="javascript:void(0);">PHP中文网</a>
This code is fine in mainstream browsers, but there will be a problem that it cannot jump under IE6. What is the reason for this?
javascript:void(0);
void(arg); can be understood as a function that always returns null, but its parameters cannot be empty. Its parameters can be any expression or even a function.
<a href="javascript:void(name = 'PHP中文网'); alert(name);">测试</a>
Test
IE6 first runs the events bound to the DOM itself, such as onclick; if bubbling is not prevented, the href attributes will be executed sequentially. And void(0); does not need to execute any events, so IE6 tells the browser not to execute any events (overwriting previous actions), and terminating bubbling is equivalent to return false; so the browser does not execute any actions. So just stop the bubbling event within the onclick event.
<a onclick="window.location.href='http://www.php.com';return false;" href="javascript:void(0);">PHP中文网</a>
This way it can run normally under IE6.
Another way is not to use javascript:void(0); but to use # instead. This can also be avoided. The # in the href attribute originally means the anchor point #name, so when If no anchor is specified, it will go to the top of the page. # has a specific meaning, and the default is #top. If there is content after #, it will be considered a tag and the corresponding tag will be jumped to there if found on the page. If it cannot be found, it will jump to the top of the page. If you do not want to jump, , you can use
,is a meaningless label specification.
I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
htmlHow to use marquee to realize text scrolling left and right
How to add images to radio and multi-select buttons Add style
The above is the detailed content of How to use the href attribute and onclick event of a tag. For more information, please follow other related articles on the PHP Chinese website!