简体   繁体   English

jQuery html函数不适用于对象作为参数

[英]jQuery html function doesn't work with object as parameter

In my application the html for picture is saved in itemConfig.picture 在我的应用程序中,图片的html保存在itemConfig.picture

If I add the following code to js: 如果我将以下代码添加到js中:

$('.picture').html(itemConfig.picture);

The div with class picture remains empty. 具有类图片的div保持为空。 But this one works: 但这一项有效:

var tmpVar='<div class="field field-name-field-picture field-type-image field-label-hidden"><div class="field-items"><div class="field-item even"><img typeof="foaf:Image" src="http://localhost/ae/sites/default/files/DSC_02911_0.jpg" width="130" height="120" alt="" /></div></div></div>'
$('.picture').html(tmpVar);

These info would be useful: 这些信息将非常有用:

alert(typeof itemConfig.picture); // returns "String"
alert(itemConfig.picture); // returns <div class="field field-name-field-picture field-type-image field-label-hidden"><div class="field-items"><div class="field-item even"><img typeof="foaf:Image" src="http://localhost/ae/sites/default/files/DSC_02911_0.jpg" width="130" height="120" alt="" /></div></div></div>

Why passing object property doesn't work? 为什么传递对象属性无效?

我认为这对您有用,但这只是一个猜测。

$('.picture').html(itemConfig.picture.toString());

You should not need .toString() as you can see in this Fiddle . 如本小提琴中所示 ,您不需要.toString()

var itemConfig = {};
itemConfig.picture = "<div style='width: 20px; height: 20px; background-color: red'></div>";

$('#start').on('click', function () {
    $('#type').html(typeof itemConfig.picture);
    $('#length').html($('.picture').length);
    $('.picture').html(itemConfig.picture);
});

The assumptions of the comments seem to be correct. 评论的假设似乎是正确的。 You either don't have an element with the class picture in the dom ( picture.length = 0 ) at this very moment or the the value of itemConfig.picture is something else. 此时,您要么在dom中没有类picture的元素( picture.length = 0 ),要么itemConfig.picture的值是其他内容。

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

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