简体   繁体   English

奇怪的DOM树JavaScript问题

[英]Odd DOM tree JavaScript problem

I am having an issue with this site - 我对这个网站有疑问 -

http://thestamp.umd.edu/canvastest/test.html http://thestamp.umd.edu/canvastest/test.html

I'm trying to build a simple HTML emailer with JavaScript to render and generate HTML which will then be sent to a page, etc. 我正在尝试使用JavaScript构建一个简单的HTML电子邮件程序来呈现和生成HTML,然后将其发送到页面等。

However, when you create three paragraphs (箱を作る) and then try to move the third one up with the onclick being swapup('render2'); 但是,当你创建三个段落(箱を作る)然后尝试将第三个段落移动时onclick为swapup('render2'); it just moves it to the top. 它只是将它移到顶部。

But the odd thing is when you paste swapup('render2'); 但奇怪的是当你粘贴掉掉('render2'); in the console, it works just fine. 在控制台中,它工作得很好。 Why should the onclick behaviour be different when I'm not referencing 'this' in any capacity? 当我没有以任何身份引用“this”时,为什么onclick行为会有所不同?

Your problem is that you are calling swapup twice. 你的问题是你两次调用swapup。 Take a look at the HTML 看看HTML

<span class="floatbutton" onclick="swapup('render0');">
    <input onclick="swapup('render2');" value="上" type="button">
    <input onclick="swapdown();" value="下" type="button">
</span>

both span and input are calling swapup. span和input都调用swapup。 It looks like swapup is being dynamically assigned at some point to the span, which may be causing your problem... I'll have to dig a little more to figure it out though. 看起来交换是在某个时刻动态分配到跨度,这可能会导致你的问题...我将不得不再挖掘一点来解决它。

EDIT: 编辑:

I see now. 我现在明白了。 In your swapup function you are calling 在您调用的交换功能中

elm.childNodes[0].setAttribute('onclick', 'swapup(\'' + previous.id + '\');');

I'm not sure if you intended that or not... can you clarify? 我不确定你是否打算这样做......你能澄清一下吗?

EDIT 2: 编辑2:

I think you need to change 我想你需要改变

elm.childNodes[0].setAttribute('onclick', 'swapup(\'' + previous.id + '\');');
previous.childNodes[0].setAttribute('onclick', 'swapup(\'' + temp + '\');');

in boxes.js lines 23-24 to 在boxes.js第23-24行

elm.getElementsByTagName("input")[0].setAttribute('onclick', 'swapup(\'' + previous.id + '\');');
previous.getElementsByTagName("input")[0].setAttribute('onclick', 'swapup(\'' + temp + '\');');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM