在函数中,我需要检查一个(div,span,p)是否包含任何.html元素,然后再尝试删除html并添加新内容。如何在删除前检查元素是否含有HTML? jQuery
不知道如何做到这一点...
我尝试这样做,但不工作:
// HERE below I tried to do a check to see if the div's have HTML, but did not work
if ($('.'+rowName+' div').html) {
$('.'+rowName+' div').html.remove();
$('.'+rowName+' span').html.remove();
$('.'+rowName+' p').html.remove();
}
全功能
// Create the Role/Fan rows
function rowMaker (rowName, roleName) {
//alert(rowName+' '+roleName);
// HERE below I tried to do a check to see if the div's have HTML, but did not work
if ($('.'+rowName+' div').html) {
$('.'+rowName+' div').html.remove();
$('.'+rowName+' span').html.remove();
$('.'+rowName+' p').html.remove();
}
// Blue button
$('.'+rowName+' div').append(roleName);
$('.'+rowName+' div').attr('id', 'blue-fan-'+roleName);
var blueButton = ('blue-fan-'+roleName);
console.log('blueButton = '+blueButton);
// Genres
$('.'+rowName+' span').append(roleType);
// Tags
$.each(role_Actor, function(index, item) {
$('.'+rowName+' p').append(item+', ');
});
$('#'+blueButton).click(function() {
console.log('clicked blue button');
// clears the role_Actor to be used to recapture checkboxes
role_Actor = [];
console.log('role_Actor = '+role_Actor);
//$('#modal-'+roleId).modal();
$('#modal-'+roleId).modal({persist:true});
return false;
});
}
如果元素有HTML,如果你只是要删除它,它真的很重要吗?为什么不直接用['.html'](http://api.jquery.com/html)替换内容? – 2013-04-05 02:27:31
哦,因为有些用户会按下该按钮,启动该功能,蓝色按钮还没有创建...所以没有任何.html删除和抛出和控制台上的错误:(可能而不是删除是否有某种明确的呼叫,我可以做?现在看嗯 – 2013-04-05 02:28:45