原型函数同一类的不同id元素
我正在使这个原型脚本正常工作:
function removing () {
*var ids = $$('.class').collect(function(el) { return el.id; });* //array of ids, not used by now
var data = $$('.class').pluck('innerHTML');
var wanted_count = 10;
var output = cutHtmlString(data, wanted_count);
$$('.class').invoke('replace', output + '...');
}
在此标记上:
<div class="class" id="id1">content 1</div>
<div class="class" id="id2>content 2</div>
<div class="class" id="id3>content 3</div>
cutHtmlString 是一个剪切 html 字符串并保留标签的函数,我想将其应用于每个不同的 div 内容。我从一般 $$(.class) 创建“数据”变量(通过innerHTML),然后按字数计数(var Want_count)执行切割到“输出”变量。
最后,我替换原始元素(通过$$.class 再次)及其新剪切内容。
除了一个问题之外,一切都很好:仅使用 $$(class) 使得仅考虑和替换第一个元素内容 给其他人。
我猜我也需要通过 ids 来识别元素..因此我在(收集)上添加了注释功能,但我不知道如何加入我的目标..
到目前为止我只得到了这个结果:
这是第一堂课的内容 div...(剪切)
这是第一个“class”div 的内容...
这是第一个“class”div的内容...
而我想得到:
这是第一堂课的内容 分区...
这是第二个“类”div 的内容...
这是第三个“类”div 的内容...
谢谢大家,并对整个社区表示赞赏
I'm making this prototype script to work:
function removing () {
*var ids = $('.class').collect(function(el) { return el.id; });* //array of ids, not used by now
var data = $('.class').pluck('innerHTML');
var wanted_count = 10;
var output = cutHtmlString(data, wanted_count);
$('.class').invoke('replace', output + '...');
}
on this markup:
<div class="class" id="id1">content 1</div>
<div class="class" id="id2>content 2</div>
<div class="class" id="id3>content 3</div>
cutHtmlString is a function that cut html strings keeping tags and I want to apply it to every different div content. I create 'data' variable (by innerHTML) from general $$(.class) and then I execute cutting by a words count (var wanted_count) into 'output' var.
At the end I replace the original element (by $$.class again) with its new cut content.
All works great except for one issue: using only $$(class) makes only the first element content to be considered and replaced to every others.
I need to identify elements by ids too, I guess..thus I added the commented function upon (collect) but I don't know how to join my target..
By now I only got to this results:
this is the content of firts 'class'
div...(cut)
this is the content of firts 'class' div...
this is the content of firts 'class' div...
while I want to get:
this is the content of first 'class'
div...
this is the content of second 'class' div...
this is the content of third 'class' div...
thanks guys and an appreciation to this great community whole
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您正在混合处理一组元素和处理单个元素。自从我使用 Prototype 以来已经有一段时间了,但是您是否尝试过单独进行工作,以便输出范围仅限于您要替换的元素:
I believe you are mixing working on a collection of elements with working on a single element. It's been a while since I've been in Prototype, but have you tried to work on very individually so output is scoped to the element you are replacing:
仅当您向每个元素填充相同的变量时,
Invoke
才会起作用。您需要使用each
。另外我认为你想使用
更新
而不是替换
。试试这个。
Invoke
only woks if your padding the same variables to each element. You need to useeach
.Also I think you want to use
update
and notreplace
.try this.