PrototypeJS Element.prototype.appendChild 问题 [英] PrototypeJS Element.prototype.appendChild issue

查看:74
本文介绍了PrototypeJS Element.prototype.appendChild 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Ajax 加载的内容,我不得不更改一个函数:

document.observe('dom:loaded',function() {$$('.userListHeaderCell').invoke('observe', 'click', function() {this.toggleClassName('desc');var colnumber = this.up().childElements().indexOf(this);var content = this.up(2);sortColumn(content, colnumber, this.hasClassName('desc'));});});

致:

document.observe('click', function(e, el) {if (el = e.findElement('.userListHeaderCell.sortable')) {el.toggleClassName('desc');var colnumber = el.up().childElements().indexOf(el);var content = el.up(2);sortColumn(content, colnumber, el.hasClassName('desc'));}});

sortColumn 看起来像:

function sortColumn(content, colnumber, desc) {content.select('.userListRow').sort(function(a,b){var atext = a.down(colnumber).innerHTML.stripTags().toLowerCase();var btext = b.down(colnumber).innerHTML.stripTags().toLowerCase();返回 atext.localeCompare(btext) * (desc ? -1 : 1);}).each(Element.prototype.appendChild, content);}

toggle 和 colnumber 工作和内容是一个对象 HTMLDivElement,但似乎我需要转换内容变量或使用另一个函数,但不确定 sortColumn 中的内容.我得到的错误是:

"无法转换 JavaScript 参数" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS 框架 :: https://www.someurl.com/scripts/prototype.js :: :: line 804" 数据:无]

我需要改变什么才能使这项工作发挥作用?

谢谢.

IE 中的错误显示:无法获取属性 'call' 的值:对象为 null 或未定义的prototype.js,第 804 行字符 9

我认为问题出在 Element.prototype.appendChild

尝试添加 Element.extend(content),但似乎没有帮助.

示例数据:

<div class="userListHeader"><div class="userListHeaderCell col1 sortable">名字</div><div class="userListHeaderCell col2 sortable">Last Name</div>

<div id="contentbox_People"><div class="userListRow"><div class="userListCell col1">John</div><div class="userListCell col2">Smith</div>

<div class="userListRow"><div class="userListCell col1">Bob</div><div class="userListCell col2">Ray</div>

<div class="userListRow"><div class="userListCell col1">Fred</div><div class="userListCell col2">Jones</div>

解决方案

既然是我的代码出错了,我应该试试.

线,

}).each(Element.prototype.appendChild, content);

是一种简写方式,

}).each(function(row) {content.appendChild(row);});

您可以尝试这种方式,看看情况是否更好.我不认识这个错误,但一个快速的谷歌表明它来自 Firefox,并且与将非元素传递给期望一个的本机函数有关.在这种情况下,我倾向于在任何地方粘贴大量 console.log(...) 调用(并在 Firefox 中按 F12),看看会发生什么.记录数组应该将所有行显示为 HTML 元素.

Due to Ajax loaded content, I had to change a function from:

document.observe('dom:loaded',function() {    
    $$('.userListHeaderCell').invoke('observe', 'click', function() {       
        this.toggleClassName('desc');        
        var colnumber = this.up().childElements().indexOf(this);        
        var content = this.up(2); 
        sortColumn(content, colnumber, this.hasClassName('desc'));    
    });
});

TO:

document.observe('click', function(e, el) {  
    if (el = e.findElement('.userListHeaderCell.sortable')) { 
        el.toggleClassName('desc');        
        var colnumber = el.up().childElements().indexOf(el);        
        var content = el.up(2); 
        sortColumn(content, colnumber, el.hasClassName('desc'));   
    }
});

The sortColumn looks like:

function sortColumn(content, colnumber, desc) {    
    content.select('.userListRow').sort(function(a,b){        
        var atext = a.down(colnumber).innerHTML.stripTags().toLowerCase();        
        var btext = b.down(colnumber).innerHTML.stripTags().toLowerCase();        
        return atext.localeCompare(btext) * (desc ? -1 : 1);    
    }).each(Element.prototype.appendChild, content);
}

The toggle and colnumber work and content is an object HTMLDivElement, but it seems that I need to convert the content variable or use another function, but not sure what in the sortColumn. The error I get is:

"Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: https://www.someurl.com/scripts/prototype.js :: :: line 804" data: no]

What do I need to change in order to make this work?

Thanks.

EDIT:

Error in IE shows: Unable to get value of the property 'call': object is null or undefined prototype.js, line 804 character 9

I believe the issue is with Element.prototype.appendChild

Tried adding Element.extend(content), but doesn't seem to help.

Example data:

<div id="contentbox_Users" class="userList">
    <div class="userListHeader">
        <div class="userListHeaderCell col1 sortable">First Name</div>
        <div class="userListHeaderCell col2 sortable">Last Name</div>
    </div>
    <div id="contentbox_People">
        <div class="userListRow">
            <div class="userListCell col1">John</div>
            <div class="userListCell col2">Smith</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Bob</div>
            <div class="userListCell col2">Ray</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Fred</div>
            <div class="userListCell col2">Jones</div>
        </div>
    </div>
</div>

解决方案

Since it is my code that is failing I should have a go.

The line,

}).each(Element.prototype.appendChild, content);

is a shorthand way of saying,

}).each(function(row) {
    content.appendChild(row);
});

You can try it that way to see if things are better. I don't recognise the error but a quick google suggests it is from Firefox and relates to passing a non-element to a native function that expects one. In situations such as this I tend to stick lots of console.log(...) calls everywhere (and press F12 in Firefox) and see what happens. Logging the array should show all the rows as HTML elements.

这篇关于PrototypeJS Element.prototype.appendChild 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆