帮助转换内联 onclick
我正在尝试将一些硬编码的内联 onclick 转换为动态创建。
它们现在的样子如下:
(html)
<ul id="search-navlist">
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'A'); return false;">A</a></li>
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'B'); return false;">B</a></li>
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'C'); return false;">C</a></li>
....
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'Z'); return false;">Z</a></li>
</ul>
这是新代码:
(脚本)
$(document).ready(function() {
$('#search-navlist li a').each(function() {
$(this).attr("onclick", "searchExhibitorsByAlphabet(this,'A'); return false;");
});
});
(html 格式)
<ul id="search-navlist">
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
....
<li><a href="#">Z</a></li>
</ul>
到目前为止,尽管所有链接都相同,但效果很好。我希望链接与其各自的信件相关联。
我发现了这个功能:
function iterateAlphabet() {
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(var i=0; i<str.length; i++) {
var nextChar = str.charAt(i);
alert(nextChar)
}
}
...但不确定如何合并它。 (我还看到一些使用字符代码的脚本,上面的函数对我来说更容易理解。)
我希望得到一些帮助。在此期间我会继续努力。
谢谢。
I'm trying to convert some hard-coded inline onclicks to be dynamically created.
Here's what they look like now:
(html)
<ul id="search-navlist">
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'A'); return false;">A</a></li>
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'B'); return false;">B</a></li>
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'C'); return false;">C</a></li>
....
<li><a href="#" onclick="searchExhibitorsByAlphabet(this,'Z'); return false;">Z</a></li>
</ul>
Here's the new code:
(script)
$(document).ready(function() {
$('#search-navlist li a').each(function() {
$(this).attr("onclick", "searchExhibitorsByAlphabet(this,'A'); return false;");
});
});
(in html)
<ul id="search-navlist">
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
....
<li><a href="#">Z</a></li>
</ul>
So far, this works fine, although all of the links are the same. I'd like the links to be associated with it's respective letter.
I found this function:
function iterateAlphabet() {
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(var i=0; i<str.length; i++) {
var nextChar = str.charAt(i);
alert(nextChar)
}
}
...but am uncertain as to how to incorporate it. (I also saw some script using charcodes, the above function was easier for me to understand.)
I'd appreciate some help. I'll continue trying in the meantime.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以重写为:
DEMO
你也可以使用 delegate 来更好地性能:
演示
You can rewrite as:
DEMO
You can also use delegate for better performance:
Demo
您可以执行以下操作:
将 HTML 更改为:
Here is what you can do:
And change your HTML to: