2016-07-13 83 views
0

我想搜索xhr.responseText爲一個id爲「something」的div,然後從該div中包含的xhr.responseText中刪除所有內容。JS搜索返回div的xhr.responseText,然後刪除div

這裏是我的代碼XHR:

function getSource(source) { 
    var url = source[0]; 
    var date = source[1]; 
    /****DEALS WITH CORS****/ 
    var cors_api_host = 'cors-anywhere.herokuapp.com'; 
    var cors_api_url = 'https://' + cors_api_host + '/'; 
    var slice = [].slice; 
    var origin = self.location.protocol + '//' + self.location.host; 
    var open = XMLHttpRequest.prototype.open; 
    XMLHttpRequest.prototype.open = function() { 
     var args = slice.call(arguments); 
     var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]); 
     if (targetOrigin && targetOrigin[0].toLowerCase() !== origin && 
      targetOrigin[1] !== cors_api_host) { 
      args[1] = cors_api_url + args[1]; 
     } 
     return open.apply(this, args); 
    }; 
    /****END DEALS WITH CORS****/ 
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", cors_api_url+url, true); 
    xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
      var resp = xhr.responseText; 
      var respWithoutDiv = removeErroneousData(resp); 
     } 
     else{ 
      return "Failed to remove data."; 
     } 
    } 
    xhr.send(); 
} 

刪除DIV這裏:

/*This must be in JS not JQUERY, its in a web worker*/ 
function removeErroneousData(resp) { 
    var removedDivResp = ""; 

    /*pseudo code for removing div*/ 
    if (resp.contains(div with id disclosures){ 
     removedDivResp = resp.removeData(div, 'disclosures'); 
    } 

    return removedDivResp; 
} 

回答

0

你可以轉儲一個div的響應,然後搜索該div你想清空其內容。

xhr.onreadystatechange = function(){ if(xhr.readyState == 4)var resp = xhr.responseText; $('div')。attr('id','resp')。html(resp); ('#resp')。find('disclosure')。html(''); // var respWoutoutDiv = removeErroneousData(resp); } else { return「無法刪除數據。」; } }

+0

我需要它在JS而不是JQuery。謝謝你的迴應。 – vector