检查JSON是否为img标签并在JavaScript中更改src

罗素·哈勒(RussellHarrower)

我们有一个定制的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);

}

我们需要看的部分是news.post_content

编辑:添加了JSON看起来像

"post_content":"<p>DR SHARON Giese is a household name in America, starring on hit medical shows Dr Oz and The Doctors and making regular appearances on CBS News, CNN and NBC.<\/p>\n<p>Dr Giese is ranked among the best in her field but, as one family discovered \"with tragic consequences\" even the best make mistakes.<\/p>\n<center><img src=\"..\/..\/..\/upload\/Health\/Dr Sharon Giese - Dr OZ - YouTube.jpg\" alt=\"Dr Oz Show - DR SHARON\" width=\"650\" height=\"488\" \/><\/center>\n<div style=\"padding: 0; margin: -6px 0 4px 10px; text-align: left;\">A screen grab from a recent appearance on the Dr Oz show. Picture: YouTube Source: YouTube<\/div>\n<p>In June 2009, 32-year-old mother-of-two Adriana Porras underwent liposuction, a fat reduction procedure performed by Dr Giese. Ms Porras died two days later of a pulmonary embolism.<\/p>\n<p>Her husband Pablos Balzola then sued Dr Giese for negligence, claiming she did not return their frantic calls when Ms Porras showed signs of severe post-surgery complications, complaining of chest pains and shortness of breath.<\/p>\n<p>In court, pathologists testified that had she received treatment for those symptoms, her chance of survival would have been much higher.<\/p>\n<p>It was later found out that Dr Giese had not performed the procedure in a hospital but in her own home office.<\/p>\n<p>This week she finally reached a settlement with Mr Balzola, agreeing to pay US$2.3 million (AUD$2.5 million)<\/p>\n<p>The money will reportedly be split between the widower, his six-year-old daughter Maia and nine-year-old son Nicholas. Mr Balzola"

我已将此代码用于我们的feature_images

 if(news.featured_image.substring(0, 7) !== "http://")
        {
        news.featured_image = "http://www.radiobreakout.com.au/"+news.featured_image;
        }

但是如何获取post_content中的img srcs。

奥利·K

请考虑以下几点:

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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用javascript检查html img src是否为空

解析JSON中的<img src>标签-iOS

无法在 Javascript 中更改 img 的 src(无法将属性“src”设置为 null)

通过src值查找img标签,并在img标签中添加新属性

等待 img src 在 selenium (JavaScript) 中更改?

Javascript获取img src并在div中显示链接

检查Javascript数组中是否存在标签

以编程方式更改img标签的src

从数组列表中替换 IMG src 并在 HTML 中显示更改

通过单击按钮更改javascript中的img src

如何使用JavaScript检查ul标签是否为空?

JavaScript:检查html p标签是否为空

在悬停时更改多个 img 标签的 img src 属性

如何使用 jquery/javascript 在图像标签 <img src=""> 的 src 属性中传递字符串?

检查value是否为JavaScript中的Symbol

检查JavaScript中变量是否为空?

Ruby:在JavaScript中检查是否为零

检查数字是否为javascript中的复数

使用JavaScript设置img标签的src属性

如何检查字符串是否包含子字符串并在javascript中为其着色?

如何检查 A 标签是否环绕 IMG 元素?

出于某种奇怪的原因,用于检查 svg HTML 标签的 javascript RegExp 正在测试“img”是否为真,这是怎么回事?

如何从VBA中的img标签解析src

img 标签从 src 中删除斜杠

是否可以在CSS中设置img标签的src属性的等效项?

在 React 中以编程方式检查 img src

使用javascript单击更改img src

如何使用JavaScript更改img src?

Javascript不更改<img> src属性?