Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

javascript - JS将onclick()转换为鼠标悬停(JS convert onclick() to mouseover)

I'm having an issue with a piece of JS that doesn't seem to want to work.(我遇到了一个似乎不起作用的JS问题。)

I created a tab widget, following this guide: https://www.w3schools.com/howto/howto_js_tabs.asp(我按照此指南创建了一个标签小部件: https : //www.w3schools.com/howto/howto_js_tabs.asp) I replaced onclick with onmouseover in the html part and changed the buttons to a-type elements:(我将html部分中的onclick替换为onmouseover,并将按钮更改为a型元素:) Original:(原版的:) <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button> My version:(我的版本:) <a class="tablinks" onmouseover="openCompany(event, 'xName') "id="defaultOpen">Link</a> Now that's all working fine.(现在一切正常。) But further down the page it explains how to keep a certain tab open by default but it doesn't seem to be working alright.(但是在页面的下方,它解释了如何在默认情况下保持某个选项卡处于打开状态,但是似乎无法正常工作。) Neither in the original nor in my version.(既不是原始版本也不是我的版本。) Original:(原版的:) document.getElementById("defaultOpen").click(); My version:(我的版本:) document.getElementById("defaultOpen").mouseover(); Has anyone got an idea why this wouldn't work?(有谁知道为什么这行不通?) It's driving me nuts!(真让我发疯!)   ask by Nostra Damus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this solution(试试这个解决方案)

Just use(只需使用) document.getElementById("defaultOpen").onmouseover(); instead of(代替) document.getElementById("defaultOpen").mouseover(); function click_btn() { document.getElementById("defaultOpen").onmouseover(); } function openCompany(a, b) { alert('link'); } <button class="tablinks" onclick="click_btn()" id="defaultOpenButton">London</button> <a class="tablinks" onmouseover="openCompany(event, 'xName')" id="defaultOpen">Link</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...