简体   繁体   English

使用Json加载本地图像

[英]Load local image using Json

I trying to redo a site someone has. 我试图重做某人拥有的网站。 I am trying to load a local image to the javascript but with no luck. 我正在尝试将本地图像加载到javascript,但是没有运气。 Here is the JSON file. 这是JSON文件。 I just included the image file in the third line. 我只是在第三行中包含了图像文件。

{"list":
[
"Event Title 1","Description 1","Date 1",
"Event 2","Stuff happening 2","Date 2",
"Event 3","gallery/photo2.jpg","Date 3"
]
}

And here is the script 这是脚本

      for(var i=0;i<9;i+=3){
        str=eventarray[i+2];
        var dt = document.createElement("h2");
        //dt.className="date2";
        //dt.style.backgroundColor="purple";
        dt.style.color="white";
        dt.style.fontSize="30px";

        var t2=document.createTextNode(str);
        dt.appendChild(t2); 
        dt.id="evnt"+ i;

        var brk = document.createElement("br");
        var brk2 = document.createElement("br");

        str = eventarray[i];
        var ev=document.createElement("h1");
        //ev.className="edate";
        ev.style.fontSize="35px";
        ev.id = "event" + i.toString();
        var t=document.createTextNode(str);
        ev.appendChild(t); 

        str = eventarray[i+1];
        var des=document.createElement("p");
        //des.className="edate";
        des.style.fontSize="16px";
        des.style.marginTop="10px";
        des.id = "des" + i.toString();
        var t2=document.createTextNode(str);
        des.appendChild(t2); 

        var box = document.createElement("div");
        box.style.backgroundColor="white";
        box.style.borderRadius="10px"
        box.style.textAlign="center"
        box.style.marginBottom="100px";
        //box.className = "edate";
        var dt2 = document.createElement("div");
        dt2.style.minWidth = "100%";
        dt2.style.backgroundColor= "#c59ed1";
        dt2.appendChild(dt);
        box.appendChild(dt2);
        box.appendChild(brk);
        box.appendChild(ev);
        box.appendChild(brk2);
        box.appendChild(des);
            document.getElementById("events").appendChild(box);
    }

Here's what it looks on site: 这是网站上的外观: 在此处输入图片说明

I'm not to familiar with JSON but with little JQuery skills I have I tried to replace the creatTextNode with createElement and then tried to append the str variable to the new Element but the whole page broke. 我对JSON不熟悉,但是对JQuery的了解很少,我试图用createElement替换creatTextNode,然后尝试将str变量附加到新Element上,但是整个页面都坏了。 Any guidance will be greatly appreciated. 任何指导将不胜感激。

The problem here is you cannot just "append" your str variable to just any element - you must assign it to the src property of an HTMLImageElement (there is no src on (most) other element types). 这里的问题是您不能仅仅将str变量“附加”到任何元素上-您必须将其分配给HTMLImageElementsrc属性((大多数)其他元素类型上没有src )。 The other problem is that you currently have no way of knowing if your description text is just plain text or an img url. 另一个问题是,您目前无法知道描述文字是纯文本还是img url。 Either you need to build in this distinction to your json list, or test for it and build your page accordingly: 您需要在json列表中建立这种区别,或者对其进行测试并相应地构建页面:

if ( str.match( /\.(?:jpe?g|png|tiff|gif)$/ ) ) {

    var t2 = document.createElement( 'img' );
    t2.src = str;

} else {

    var t2=document.createTextNode(str);

}

This very quick-n-dirty test creates t2 as an image if it detects a image file extension at the end of the str string, or a text node if it does not. 如果检测到str字符串末尾的图像文件扩展名,则此t2 -dirty快速测试将t2创建为图像,否则将文本节点创建为文本节点。 It's a terrible way to achieve your goal - this is an example - find a more robust way. 这是实现目标的糟糕方法-这是一个例子-找到更可靠的方法。

Here's a fiddle demonstrating: http://jsfiddle.net/tN9mC/ 这是一个小提琴演示: http : //jsfiddle.net/tN9mC/

Another problem is the url you have in your example does not point to a "local" file - for that you would need to give a fully-qualified url like "file:///C:/Users/public/documents/images/gallery/photo2.jpg" etc. Only using "gallery/photo2.jpg" will make it a relative url that points to that directory/file on whatever domain/path the page currently is on - for example, the fiddle tries to load " http://fiddle.jshell.net/tN9mC/show/gallery/photo2.jpg " 另一个问题是您的示例中的网址未指向“本地”文件-为此,您需要提供完全限定的网址,例如“ file:/// C:/ Users / public / documents / images / gallery / photo2.jpg”等。仅使用“ gallery / photo2.jpg”会使它成为一个相对的URL,指向当前页面所在的任何域/路径上的该目录/文件-例如,小提琴试图加载“ http://fiddle.jshell.net/tN9mC/show/gallery/photo2.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM