javascript - 合并函数和抽象(JavaScript)请帮助
问题描述
我是一个早期的初学者,并且拼命想让这段代码工作。请帮助或建议我。当鼠标打开或关闭时,我正在尝试使图标悬停(弹出和弹出)。:/
**HTML**
<section id="ingredients"
onmouseover="ingredientsHover('mouseover')" onmousedown="ingredientsNormal('mousedown')">
<h2>ingredients <i class="fa fa-coffee" aria-hidden="true"></i>
</h2>
</section>
<section id="preparation" onmouseover="preparationHover('mouseover')" onmousedown="preparationNormal('mousedown')">
<a href="#" class="box">
<h2>preparation
<i class="fa fa-list-alt" aria-hidden="true"></i>
</h2>
**JAVASCRIPT**
function ingredientsHover(action) {
if (action === 'mouseover') {
document.getElementById('ingredients').firstElementChild.firstElementChild.style.fontSize = '300%';
}
else if (action === 'mousedown') {
document.getElementById('ingredients').firstElementChild.firstElementChild.style.fontSize = '100%';
}
}
}
function preparationHover(action) {
if (action === 'mouseover') {
document.getElementById('preparation').firstElementChild.firstElementChild.style.fontSize = '300%';
}
else if (action === 'mousedown') {
document.getElementById('preparation').firstElementChild.firstElementChild.style.fontSize = '100%';
}
}
解决方案
您可以在调用函数时传递一个附加参数,然后有条件地更改您的样式。
HTML:
<section id="ingredients" onmouseover="ingredientsHover(this,'mouseover')" onmousedown="ingredientsNormal(this,'mousedown')"></section>
JS:
function doSomething(element, action) {
if(action === 'mouseover') {
element.firstElementChild.firstElementChild.style.fontSize = '300%';
} else if (action === 'mousedown') {
element.firstElementChild.firstElementChild.style.fontSize = '100%';
}
}
推荐阅读
- perl - 如何使 uc() 在 Perl 中像 toupper() 在区域感知 POSIX 环境中一样在 AWK 中工作?
- java - 尝试代码时出现此错误:线程“main”java.lang.NullPointerException 中的异常。有人能帮我吗?
- javascript - 我可以在 mat-expanded 行中显示另一个表吗?
- python-3.x - S3 Python列出嵌套的子目录
- reactjs - gapi.client.load() 在我的科尔多瓦应用程序的登录功能中不起作用
- ruby-on-rails - 如何修复 Heroku 中的“客户端请求中断”错误(使用 rails 应用程序)?
- css - 如何使折叠的导航菜单向下打开
- regex - 如何匹配多个可选组中的至少一个?
- sql-server-2014 - 具有窗口函数的时间戳之间的最小差异
- java - org.hibernate.id.IdentifierGenerationException:必须在调用 save() 之前手动分配此类的 id:com.app.entites.LDetails