我正在为一名摄影师做后台工作,那里的照片会在带有子类别的相册中。
我在相册创建页面中使用 JavaScript 添加新的 .photo block (及其 HTML 内容)。
问题是我显然得到了函数albums.addPhoto 的无限循环。这是我的 JS 代码(albums.addPhoto 由 onclick 属性调用并调用 albums.createBlock):
var albums = {
createBlock: function (tag, attributes, text, elemID, elemCLASS) {
var block = document.createElement(tag);
switch (tag) {
case "label":
block.setAttribute('for', attributes[0]);
break;
case "input":
block.setAttribute('type', attributes[0]);
block.setAttribute('name', attributes[1]);
if (attributes[0] == "text") {
block.setAttribute('placeholder', attributes[2]);
} else {
block.setAttribute('value', attributes[2]);
}
break;
default:
}
if (text) {
block.textContent = text;
}
if (elemID) {
block.id = elemID;
}
if (elemCLASS) {
block.classList.add(elemCLASS);
}
return block;
},
addPhoto: function (currentSsCat, requiredPhotoID) {
var elem = albums.createBlock("div", null, false, ("photo_" + currentSsCat + "_" + requiredPhotoID), "photo");
document.querySelector('#ss_cat_1').appendChild(elem);
elem.appendChild(albums.createBlock("label", ["file_" + currentSsCat + "_" + requiredPhotoID], "Importez le fichier photo", false, false));
elem.appendChild(albums.createBlock("input", ["file", ("file_" + currentSsCat + "_" + requiredPhotoID), "Votre photo"], false, false, false));
elem.appendChild(albums.createBlock("label", ["ref_" + currentSsCat + "_" + requiredPhotoID], "Référence de la photo", false, false));
elem.appendChild(albums.createBlock("input", ["text", ("ref_" + currentSsCat + "_" + requiredPhotoID), "Exemple: 20160811_CLS_0005"], false, false, false));
elem.appendChild(albums.createBlock("label", ["desc_" + currentSsCat + "_" + requiredPhotoID], "Description de la photo", false, false));
elem.appendChild(albums.createBlock("input", ["text", ("desc_" + currentSsCat + "_" + requiredPhotoID), "Exemple: Paolo FABBRI et Lenas Drawin His Gun (ITA)."], false, false, false));
document.querySelector("#add_photo_" + currentSsCat).onclick = albums.addPhoto(currentSsCat, (requiredPhotoID + 1));
}
};
然后你有 HTML 部分(我只给你 form 部分,我可以发送整个页面,但我认为它没有用):
<form action="index.html?page=aa" method="post">
<!-- SOUS CATEGORIE -->
<div class="ss_cat" id="ss_cat_1">
<label for="ss_cat_1_titre">Titre :</label>
<input type="text" name="ss_cat_1_titre" placeholder="Titre de la sous-catégorie...">
<!-- PHOTOS DE LA CATEGORIE -->
<div class="photo" id="photo_1_1">
<label for="file_1_1">Importez le fichier photo :</label>
<input type="file" name="file_1_1" value="Votre photo">
<label for="ref_1_1">Référence de la photo :</label>
<input type="text" name="ref_1_1" placeholder="Exemple: 20160811_CLS_0005">
<label for="desc_1_1">Description de la photo :</label>
<input type="text" name="desc_1_1" placeholder="Exemple: Paolo FABBRI et Lenas Drawin His Gun (ITA).">
</div>
</div>
<!-- This is the link calling the function albums.addPhoto -->
<a href="#" class="btn" id="add_photo_1" onclick="albums.addPhoto(1, 2)">Ajouter une photo</a>
</div>
<a href="#" class="btn" id="add_ss_cat">Ajouter un sous-catégorie</a>
<input type="submit" name="submit" value="Ajouter le nouvel album">
</form>
测试: 该函数完成了它的工作,但是有一个无限循环(浏览器崩溃或者问我是否要停止脚本,它显示了大量的 .photo block 已创建)。
在 Chrome 上,我收到消息 maximum call stack size exceeded,在 Firefox 上,我收到太多递归。
PS:这是我第一次尝试创建后台,如果您对其他方法有任何建议,请随时提出。感谢阅读和帮助。
最佳答案
这是你的问题:
document.querySelector("#add_photo_" + currentSsCat).onclick = albums.addPhoto(currentSsCat, (requiredPhotoID + 1));
您无条件地在 albums.addPhoto 中调用 albums.addPhoto,因此存在无限递归。你可能想要这个:
document.querySelector("#add_photo_" + currentSsCat).onclick = function () { albums.addPhoto(currentSsCat, (requiredPhotoID + 1)); };
这样您就可以将 onclick 处理程序设置为一个函数,而不是该函数的结果(这是未定义的,无论如何都没有意义),并且它仅在有点击事件时运行。
关于javascript - 具有递归javascript函数的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380955/
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时