代码之家  ›  专栏  ›  技术社区  ›  babbitt

moootools.get(“text”)未按预期工作

  •  3
  • babbitt  · 技术社区  · 15 年前

    :这在Firefox中有效,但在Chrome6上无效,仍在调查中。

    我正在上网络编程课,学习各种JavaScript框架。我正在处理一个moootools问题,.get(“text”)方法在responseXML上似乎没有按预期工作。

    以下是答案:

    <?xml version="1.0" encoding="UTF-8"?> 
    <users> 
            <user> 
                <username>etomai</username> 
                <first_name>Joe</first_name> 
                <last_name>Smith</last_name> 
            </user> 
            <user> 
                <username>hehhh</username> 
                <first_name>hee</first_name> 
                <last_name>lkii</last_name> 
            </user> 
    </users>
    

    代码如下:

    function update_list() {
        var req = new Request({
            url: 'users.php',
            method: 'get',
            data: {
                'term': $('search').value
            },
            onComplete: function(responseText, responseXML){
                $('list').empty();
                responseXML.documentElement.getElements('user').each(function(user){
                    //this seems like it should work according to all mootools docs, but it doesn't.  user.getElement('username').get('tag') works, but neither 'html' or 'text' work...retuturning undefineds.
                    console.log(user.getElement('username').get('text'));
                    var copy = $('template').clone();
                    $('list').grab(copy);
                    $(copy).removeClass('hidden');
                    $(copy).getChildren()[0].set("text", "username goes here")
                    $(copy).getChildren()[1].set("text", "name goes here")
                })
    
            }
        }).send();
    
    }
    

    如注释所示,在第行 console.log(... 我应该(根据文档)能够使用 .get('text') . 相反,它返回未定义。

    检查的值 user.getElement('username') 返回预期的

    <username>etomai</username>
    

    找到运行代码 here

    尝试在搜索字段中输入字母“e”。

    我被困在这个关头。有人能指出我的正确方向吗?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Anurag    15 年前

    在我看来像个虫子。关于Firefox, "text" 决定使用 textContent 属性,它是 Element .

    然而,在铬合金上,它可以 innerText 因为它支持HTML DOM,但不支持XML DOM。但我们正在这里尝试在XML DOM上使用它。

    两种浏览器都可以使用的一种解决方法是,

    user.getElement('username').textContent