我有一个javascript函数,通过在文档中添加/删除类“隐藏”来公开和隐藏html DIVs,它的外观如下所示
function Expose (castid){
if ( $('#newsitemcontent-' +castid).hasClass('hidden')){
$('#newsitemcontent-' +castid).removeClass('hidden');
$('#broadcast-' +castid).addClass('ExposeNews');
} else {
$('#newsitemcontent-' +castid).addClass('hidden');
$('#newsitemcontent-' +castid).slideUp("fast");
$('#broadcast-' +castid).removeClass('ExposeNews');
}
return false;
}此函数在已打印的div (默认情况下来自php )中运行良好,但如果任何div是使用javascript (如由
$(output).insertBefore('.broadcast:first').fadeIn('slow');它不会运行的。我认为函数不能看到从javascript打印出来的DIV的DIV I。
例如,jquery打印的html
<div id="broadcast-1" class="broadcast news highlightedbroadcast" onclick="Expose(1)">
<div class="newsitemtitle">
<a class="stopp" href="#" target="_blank" title="title">title</a>
</div>
</div>
<div id="newsitemcontent-1>" class="newsitemcontent hidden">
msg
</div>
<div class="newsitemcontentmisc">something will come here</div>
</div>而javascript像这样打印html (它使用https://github.com/janl/mustache.js/blob/master/mustache.js fo呈现来自隐藏div内容的文本作为模板)
var solobroadcast_template =$("#hidden_solobroadcast").html()
var convertedvars = {
broadcasts_ids: this.castid,
broadcasts_titles: this.casttitle,
broadcasts_castmsgs: function (){
return contentmsg;
}
};
var output = Mustache.render(solobroadcast_template, convertedvars);
$(output).insertBefore('.broadcast:first').fadeIn('slow');问题是,公开函数在已经打印的内容中运行良好,但是如果页面使用javascript打印新代码,则公开函数将工作,但隐藏类不添加或删除。
有什么建议吗?
发布于 2014-01-01 06:37:54
你的代码看上去很好。用on()试试:
<div id="broadcast-1" class="broadcast news highlightedbroadcast" data-bc="1">
$('.broadcast').on('click', function() {
Expose($(this).attr('id').split('-')[1]);
// OR
// Expose($(this).data('bc'));
});https://stackoverflow.com/questions/20865601
复制相似问题