例如,对于可访问性的原因,我想我onfocus和onmouseover具有相同的价值观.为了可维护性,我想只宣告一次.
我会喜欢能够做到这一点:
<a onmouseover,onfocus="assist('hello');">linktext</a>
Run Code Online (Sandbox Code Playgroud)
但是,当然,这太简单了,也行不通.我可以通过标签干燥的最佳方式是什么?
更好的方法是将所有事件处理程序移出HTML并进入javascript.
<a id="some_id">link text</a>
Run Code Online (Sandbox Code Playgroud)
(您不需要使用ID,只是为了便于演示.)
var link = document.getElementById('some_id');
link.onmouseover = link.onfocus = function() {
assist('hello');
};
Run Code Online (Sandbox Code Playgroud)
如果你想把它提升到一个新的水平,你可以通过很多链接来概括它.这个例子使用jQuery.
<a href="#hello" class="assist">link text</a>
<a href="#something" class="assist">another link</a>
Run Code Online (Sandbox Code Playgroud)
然后你将事件处理程序一次性应用到所有链接(非常干!)
$('a.assist').bind('mouseover focus', function() {
assist(this.href); // you'd need to clean up the href here, but you get the picture
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66 次 |
| 最近记录: |