jQuery - 用类名替换循环
function loadTextboxes()
{
var textareas = document.getElementsByTagName('textarea');
for(var i=0; i < textareas.length; i++)
{
if (textareas.item(i).className == "richtextbox")
{
richtextbox(textareas.item(i));
}
}
}
//window.attachEvent('onload',loadTextboxes);
$(document).ready(function() {
//loadTextboxes(); // works ...
$('.richtextbox').each(richtextbox(this));
});
JavaScript 函数搜索 具有类“richtextbox”的文本区域和 调用另一个函数(未发布 这里)...尝试用 jQuery 来做到这一点 - 不起作用:-(
function loadTextboxes()
{
var textareas = document.getElementsByTagName('textarea');
for(var i=0; i < textareas.length; i++)
{
if (textareas.item(i).className == "richtextbox")
{
richtextbox(textareas.item(i));
}
}
}
//window.attachEvent('onload',loadTextboxes);
$(document).ready(function() {
//loadTextboxes(); // works ...
$('.richtextbox').each(richtextbox(this));
});
A JavaScript function searches for
textarea with class "richtextbox" and
calls another function (not posted
here) ... tried to do this with jQuery
- does not work :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这一行:
意味着您调用
richtextbox(this)
并将其返回值传递给each()
。除非该函数返回一个函数,否则这将不起作用。我怀疑你的意思是:
这是将命名函数作为参数传递的正确方法。
理想情况下,
this
由函数假定,而不是作为参数传入,这将允许您将代码缩短为:The problem is this line:
means you call
richtextbox(this)
and pass it's return value intoeach()
. That won't work unless the function returns a function.What I suspect you mean is:
That's the correct way to pass a named function as a parameter.
Ideally,
this
would be assumed by the function rather than passed in as an argument, which would allow you to shorten the code to: