本文介绍了使用JavaScript的Items.push变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON数组,我操纵我的应用程序,
I have an JSON array, that i manipulate inside my app,
...
$.each(data, function(key, val) {
val = link + val;
foto = val;
foto = foto.substr(0, foto.lastIndexOf(".")) + ".jpg";
/* Visualizza */
var elem = document.getElementById("archivio-num");
elem.innerHTML = '<a href="#"><img src="' + foto + '"></a>';
elem.firstChild.onclick = function() {
cordova.exec("ChildBrowserCommand.showWebPage", val);
};
items.push('<li id="' + key + '">' + elem.innerHTML + '</li>');
});
...
$ b $ p
现在我试图将所有外部元素var elem。
Puttin only + elem +给我一个错误[objectHTMLDivElement]。
可能吗?
Now i'm trying to push all elements outside that are packed inside var elem.Puttin only + elem + give me an error [objectHTMLDivElement].Is that possible?
推荐答案
进一步利用jQuery,你可能想尝试这样:
Exploiting jQuery further, you might want to try something like this :
...
var $ul = $("<ul/>");//jQuery object containing a dummy UL element in which to accumulate LI elements.
$.each(data, function(key, val) {
var url = link + val;
var foto = url.substr(0, url.lastIndexOf(".")) + ".jpg";
var $a = $('<a/>').attr('href',url).append($("<img/>").attr('src',foto)).on('click', function() {
cordova.exec("ChildBrowserCommand.showWebPage", $(this).attr('href'));
return false;
});
$ul.append($('<li/>').attr('id',key).append($a));
});
$("#archivio-num").html($a);
...
此处,不是在数组中累加HTML,在jQuery包装的UL元素中累积,该元素可用于稍后代码中的进一步处理(例如,插入到DOM中)。
Here, instead of accumulating the HTML in an array, actual LI elements are accumulated in a jQuery-wrapped UL element, which is available for further treatment (eg. insertion into the DOM) later in the code.
这篇关于使用JavaScript的Items.push变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!