【问题标题】:How to select a div from Ajax Response using Prototype JS如何使用 Prototype JS 从 Ajax 响应中选择一个 div
【发布时间】:2014-07-03 19:49:00
【问题描述】:

我正在尝试以下代码,但仍然没有成功。

var myAjax = new Ajax.Request(
    url,
    {
        method: 'post',
        onException: function (xhr, e)
        {
            alert('Exception : ' + e);
        },
        onComplete: function (xhr)
        {
            var myHtml = xhr.responseText;

            // alert(myHtml);

            var myContent = $(myHtml).select("div#product-options-wrapper");

            alert(myContent);
        }
 });

请帮忙。提前致谢。

【问题讨论】:

  • var myHtml 是否已经包含 '.'还是字符串开头的“#”?你能提供一个jsfiddle的例子吗?
  • var myHtml 包含整个页面作为响应。我需要从其中提取 id 为 #product-options-wrapper. 的 div

标签: javascript ajax prototypejs prototype


【解决方案1】:

myHtml 仍然是一个字符串对象......我想你应该将它添加到 DOM 然后你可以选择任何东西

var myAjax = new Ajax.Request(
url,
{
    method: 'post',
    onException: function (xhr, e)
    {
        alert('Exception : ' + e);
    },
    onComplete: function (xhr)
    {
        var myHtml = xhr.responseText;

        // alert(myHtml);

        /***** append your html to any container in the document ***/

        var myContent = $(myHtml).select("div#product-options-wrapper");

        alert(myContent);
    }

});

【讨论】:

    【解决方案2】:

    因为 HTML 是一个字符串 - 就像其他人提到的尝试将其添加到容器 div 中,然后从中获取您想要的元素。仅供参考,select() 将返回与选择器匹配的元素数组,因此如果您只想要特定的 <div>,请使用 down() 方法,该方法将返回第一个。

    var myAjax = new Ajax.Request(
        url,
        {
            method: 'post',
            onException: function (xhr, e)
            {
                alert('Exception : ' + e);
            },
            onComplete: function (xhr)
            {
                var myHtml = xhr.responseText;
    
                // alert(myHtml);
    
                var myContent = new Element('div').update(myHtml);
    
                myContent = myContent.down("div#product-options-wrapper");
    
                alert(myContent);
            }
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-12
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2015-07-24
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多