将 [object HTMLCollection] 转换为 javascript 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11125825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:35:51  来源:igfitidea点击:

converting a [object HTMLCollection] into string in javascript

javascripthtmlrestzimbra

提问by Ankur Ankan

I am trying to use data extracted from a XML file by getElementByTagNameand it returns HTML Collection Objectbut I need this data for a sending a REST request so I need to get the HTML Collection Object to be converted into a string. How can it be done?

我正在尝试使用从 XML 文件中提取的数据getElementByTagName并返回,HTML Collection Object但我需要此数据来发送 REST 请求,因此我需要将 HTML 集合对象转换为字符串。怎么做到呢?

Here's more information:

以下是更多信息:

com_zimbra_om.prototype._responseHandler=
        function(response){
                try{
                    sid = response.xml.getElementsByTagName("session_id");
                    this.login_user();
                    }catch(e){
                            this._showErrorMsg(e);
                            }

Using this function I am trying to get the session_idfrom a REST response. Here sid(global variable) is the HTML Collection Object. Now when I try to use this in the next function:

使用此函数,我试图session_id从 REST 响应中获取。这里sid(全局变量)是 HTML 集合对象。现在,当我尝试在下一个函数中使用它时:

com_zimbra_om.prototype.login_user = function(){
var url = selected_server + 'services/UserService/loginUser?SID=' +
                                    sid + '&username='+
                                    selected_username +
                                    '&userpass=' + 
                                    selected_password;
                var request_url = ZmZimletBase.PROXY + AjxStringUtil.urlComponentEncode(url);

So here I am using sidwhich I need as a string.

所以在这里我使用sid我需要的字符串。

So how should I convert HTML Collection Object into string??

那么我应该如何将 HTML 集合对象转换为字符串呢?

Thanks

谢谢

回答by Esailija

With this information I can only go with

有了这些信息,我只能去

var objectHTMLCollection = document.getElementsByTagName("div"),
    string = [].map.call( objectHTMLCollection, function(node){
        return node.textContent || node.innerText || "";
    }).join("");