作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<li id="Account_Tab" class="bgrad">
<a class="bganch" title="Accounts Tab" href="/xxx/xxx">Accounts</a>
</li>
很少有其他<li>
以类似的方式标记,我如何为 anchor 标记创建一个 onclick 函数,
不像:<a onclick="function()"......>
除了内联 Javascript,还有其他方法吗?
最佳答案
您可以像这样添加处理程序:
function anchorClicked(){
console.log("clicked");
}
window.onload = function(){
var anchors = document.getElementsByClassName('bganch');
for(var i=0; i<anchors.length; i++){
anchors[i].onclick = anchorClicked;
}
};
上面的代码将点击事件添加到具有 bganch
类的元素。
其他选项:
document.getElementById('someid')
document.getElementsByTagName('a')
获取标签名称的所有 anchor 关于javascript - anchor 标签 onclick 使用不显眼的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17670048/
我是一名优秀的程序员,十分优秀!