Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查JSON中的img标记并更改JavaScript中的src_Javascript_Jquery_Html_Json_Image - Fatal编程技术网

检查JSON中的img标记并更改JavaScript中的src

检查JSON中的img标记并更改JavaScript中的src,javascript,jquery,html,json,image,Javascript,Jquery,Html,Json,Image,我们有一个定制的CRM,它将IMG保存在我们的数据库中,作为/upload/image/image.jpg,但我们也有包含完整url的图像。我需要一种方法来确定图像是否以/upload开头,并将我们的网址添加到图像src的前面,以便图像正确显示,并且不会得到没有图像的正方形 我们使用的是JSON和javascript,这是到目前为止的代码 function loadnewsstory(e) { if(newsview !== true) { document.g

我们有一个定制的CRM,它将IMG保存在我们的数据库中,作为
/upload/image/image.jpg
,但我们也有包含完整url的图像。我需要一种方法来确定图像是否以/upload开头,并将我们的网址添加到图像src的前面,以便图像正确显示,并且不会得到没有图像的正方形

我们使用的是JSON和javascript,这是到目前为止的代码

function loadnewsstory(e)
{

    if(newsview !== true)
    {
        document.getElementById("newsarticals").style.display = "block";
        document.getElementById("activecontent").style.display = "none";
        newsview = true;
    }


    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("POST","http://media.queerdio.com/mobileDevice/?uri=loadstory/"+e ,false);
    xmlhttp.send();

    var newsreponse = JSON.parse(xmlhttp.responseText);
    var news = newsreponse[0];

    document.getElementById("newsarticals").innerHTML = '<h1 class="newsheader">' + news.post_title + '</h1> <div class="newsbody">'+news.post_content+'</div>';
    window.scrollTo(0,0);

}
我已经将此代码用于我们的功能图像

 if(news.featured_image.substring(0, 7) !== "http://")
        {
        news.featured_image = "http://www.radiobreakout.com.au/"+news.featured_image;
        }
<> P>但是,我如何才能在Pixi内容中查看IMG-SRCS。

< P>请考虑如下:

var articles = document.getElementById('newsarticals');
var activeContent = document.getElementById('activecontent');
var httpUrl = /^http:/;
function loadnewsstory(e) {
    if (newsview !== true) {
        articles.style.display = 'block';
        activeContent.style.display = 'none';
        newsview = true;
    }

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST','http://media.queerdio.com/mobileDevice/?uri=loadstory/' + e , false);
    xmlhttp.send();

    // empty the articles element first
    while (articles.children.length) articles.removeChild(articles.firstChild);
    // create a document fragment out of the response
    var newsArray = JSON.parse(xmlhttp.responseText);
    var newsEl = newsArray.reduce(function htmlCreator(frag, news) {
        // create the elements properly first
        var header = document.createElement('h1');
        var div = document.createElement('div');
        header.className = 'newsheader';
        header.appendChild(document.createTextNode(news.post_title));
        // innerHTML shouldn't really be used, but we'll let it slide this time
        // next time don't store html in json
        div.innerHTML = news.post_content;
        div.className = 'newsbody';
        // loop through all of the newly added images to change the sources
        Array.prototype.forEach.call(div.querySelectorAll('img'), function sourceMangler(el) {
            if (!httpUrl.test(el.src)) el.src = 'http://www.radiobreakout.com.au/' + el.src.split('/').filter(function removeSome(part) { return (part && part !== '..'); }).join('/');
        });
        frag.appendChild(header);
        frag.appendChild(div);
        return frag;
    }, document.createDocumentFragment());
    // append the fragment to the empty element
    articles.appendChild(newsEl);
    window.scrollTo(0,0);
}

news.post\u内容是什么样子的?它是图像标签吗?如果您正在寻找以“/upload”开头的字符串的搜索和替换,那么
var str=“/upload/string”;var res=str.replace(/^\/upload/,“myurlprefix”+“$&”)应该可以。但是当我们所有的图片都在上传时,我们不能确定所有链接都是这种格式,我们需要检查它的工作原理,但在android应用程序中不工作-所以是的,它是正确的,但不要在使用Cordova的android html web应用程序上使用此代码。你能更具体地说明哪部分不工作吗,我想知道我是否可以改变这个例子来解决这个问题。
var articles = document.getElementById('newsarticals');
var activeContent = document.getElementById('activecontent');
var httpUrl = /^http:/;
function loadnewsstory(e) {
    if (newsview !== true) {
        articles.style.display = 'block';
        activeContent.style.display = 'none';
        newsview = true;
    }

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST','http://media.queerdio.com/mobileDevice/?uri=loadstory/' + e , false);
    xmlhttp.send();

    // empty the articles element first
    while (articles.children.length) articles.removeChild(articles.firstChild);
    // create a document fragment out of the response
    var newsArray = JSON.parse(xmlhttp.responseText);
    var newsEl = newsArray.reduce(function htmlCreator(frag, news) {
        // create the elements properly first
        var header = document.createElement('h1');
        var div = document.createElement('div');
        header.className = 'newsheader';
        header.appendChild(document.createTextNode(news.post_title));
        // innerHTML shouldn't really be used, but we'll let it slide this time
        // next time don't store html in json
        div.innerHTML = news.post_content;
        div.className = 'newsbody';
        // loop through all of the newly added images to change the sources
        Array.prototype.forEach.call(div.querySelectorAll('img'), function sourceMangler(el) {
            if (!httpUrl.test(el.src)) el.src = 'http://www.radiobreakout.com.au/' + el.src.split('/').filter(function removeSome(part) { return (part && part !== '..'); }).join('/');
        });
        frag.appendChild(header);
        frag.appendChild(div);
        return frag;
    }, document.createDocumentFragment());
    // append the fragment to the empty element
    articles.appendChild(newsEl);
    window.scrollTo(0,0);
}