I have an a
element which has his own href and it works perfectly. The problem is that besides an href
element i want to make an onClick
function, but when i do so, the href
is ignored:
<li><a href="#someSectionOfThePage" onClick={someFunction}>GO TO SECTION</a></li>
Using this syntax, the someFunction
runs, but i'm not being redirected to #someSectionOfThePage
.
This should do the trick:
<li onClick={someFunction}>
<a href="#someSectionOfThePage">GO TO SECTION</a>
</li>
When you click the <li>
it will execute the function... and when you click the <a>
it will process the href
. Since you'll be clicking this element, both will work the same time.
The href
present in the a tag is not going to work because of the onClick
event. After clicking the element, instead of redirecting you to the section of the page, it executes someFunction()
. You may try adding location.href = "#someSectionOfThePage"
to someFunction()
which will do the work of href
.
您也可以尝试为li
添加一个click event
处理程序,并确保li
和anchor标签a
width
相同。
Although it is not advisable but if you still want to use both then this might do the trick:
someFunction= ()=>{
window.location.href("#someSectionOfThePage");
//do your onClick stuff here
};
<li>
<span onClick={this.someFunction}>GO TO SECTION</span>
</li>
Hope this helps!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.