简体   繁体   English

将变量从javascript传递回HTML?

[英]Passing a variable from javascript back to HTML?

I'm passing variables back to html, and for pictures or links, its working fine.. But i want just the "text" of the 'username' field to show. 我正在将变量传递回html,并且对于图片或链接,其工作正常。。但是我只想显示“用户名”字段的“文本”。 Here's what my javascript is doing: 这是我的JavaScript正在执行的操作:

$.each(data, function(i, item) {
                var ncode = '<div id="box"><div id="box.mainimg"><a href="'+data[i].url+'" target="_blank"><img src="'+data[i].thumb+'"></a></div><div id="box.footer"><img src="'+data[i].avatar+'"><a href="'+data[i].profilelink'" target="_"blank">"'+data[i].username'"</a></div></div>';
                $(container).append(ncode);
            });

It passes the .thumb, .avatar, as photos just fine, but when i go to link the user's actual name (which is clickable to the URL) it errors out. 它通过.thumb,.avatar,就像照片一样好,但是当我链接用户的真实姓名(可单击URL)时,它会出错。 I'm sure its a simple one, but I can't figure it out. 我敢肯定它很简单,但是我无法弄清楚。

you are missing '+' symbol at the end while appending the variables data[i].profilelink, data[i].username. 在附加变量data [i] .profilelink,data [i] .username时,结尾处缺少'+'符号。 Also you can directly access the element using item inside the loop like below. 您也可以使用循环内的item直接访问元素,如下所示。

 $.each(data, function(i, item) {
        var ncode = '<div id="box"><div id="box.mainimg"><a href="'+item.url+'" target="_blank"><img src="'+item.thumb+'"></a></div><div id="box.footer"><img src="'+data[i].avatar+'"><a href="'+item.profilelink+'" target="_"blank">"'+item.username+'"</a></div></div>';
        $(container).append(ncode);
    });

You forgot to add a + at the end of the username and profilelink 您忘记在用户名和个人资料链接的末尾添加+

$.each(data, function(i, item) {
                var ncode = '<div id="box"><div id="box.mainimg"><a href="'+data[i].url+'" target="_blank"><img src="'+data[i].thumb+'"></a></div><div id="box.footer"><img src="'+data[i].avatar+'"><a href="'+data[i].profilelink+'" target="_"blank">"'+data[i].username+'"</a></div></div>';
                $(container).append(ncode);
        });

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

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