我的菜单是通过使用web服务返回的javascript来构建的,如下所示:
strGlobalNav='<ul><li><a href="//testonline/" alt="test Online Main Page">testOnline</a></li><li><a href="//testonline/ec/" alt="Employee Center Site">Employee Center</a><ul><li><a href="//testonline/ec" alt="Employee Center Site">Employee Center</a></li><li><a href="//testonline/ec/awards" alt="Awards & Recognition Site">Awards & Recognition</a></li><li><a href="//testonline/hr/benefits" alt="Benefits Site">Benefits</a></li><li><a href="//testonline/hr/EERelations/Pages/Default.aspx" alt="Employee Relations">Employee Relations</a></li><li><a href="//testonline/hr/Employment/Pages/Default.aspx" alt="Employment">Employment</a></li><li><a href="//testonline/ec/training" alt="test University Site">test University</a></li><li><a href="//testonline/ec/healthandsafety" alt="Health & Safety Site">Health & Safety</a></li><li><a href="//testonline/sites/offices" alt="Office Locations Site">Office Locations</a></li><li><a href="//testonline/ec/travel" alt="Travel Site">Travel</a></li><li><a href="//testonline/ec/tso" alt="Total Service Organization">TSO</a></li><li><a href="//testonline/ec/vidconf" alt="Video Conferencing Services">Video Conferencing</a></li></ul></li></ul>'; document.getElementById('test-nav-wrapper').innerHTML = strGlobalNav;我需要将类添加到包含<ul><li>的父列表项的子菜项中。
这是我的导航。
<div class="globalNav">
<div id="test-nav-wrapper"></div>
</div>下面是实际的小提琴:http://jsbin.com/urIlAlO/1/edit
为什么我的脚本不能将类添加到菜单项中。我正在尝试将下拉菜单转换为两个列式超级菜单样式的东西。
请帮助我理解为什么向动态内容添加类不能工作,但是,如果我直接添加html,它会工作吗?这里的例子:http://jsbin.com/iHaqihI/1/edit
发布于 2013-10-11 16:42:54
只是一个装载顺序的问题。您可以在DOM准备好时触发jQuery,但可能在添加nav之前,因为它是由javascript添加的。如果在添加类之前将添加的内容移到document函数中,它应该可以工作。看这把更新的小提琴。
http://jsbin.com/urIlAlO/8/
我也把所有的逻辑从头部部分移到身体上,所以它是非阻塞的。实际上,我所做的是将您的文档就绪功能更改为以下内容:
$(function() {
var strGlobalNav='<ul><li><a href="//testonline/" alt="test Online Main Page">testOnline</a></li><li><a href="//testonline/ec/" alt="Employee Center Site">Employee Center</a><ul><li><a href="//testonline/ec" alt="Employee Center Site">Employee Center</a></li><li><a href="//testonline/ec/awards" alt="Awards & Recognition Site">Awards & Recognition</a></li><li><a href="//testonline/hr/benefits" alt="Benefits Site">Benefits</a></li><li><a href="//testonline/hr/EERelations/Pages/Default.aspx" alt="Employee Relations">Employee Relations</a></li><li><a href="//testonline/hr/Employment/Pages/Default.aspx" alt="Employment">Employment</a></li><li><a href="//testonline/ec/training" alt="test University Site">test University</a></li><li><a href="//testonline/ec/healthandsafety" alt="Health & Safety Site">Health & Safety</a></li><li><a href="//testonline/sites/offices" alt="Office Locations Site">Office Locations</a></li><li><a href="//testonline/ec/travel" alt="Travel Site">Travel</a></li><li><a href="//testonline/ec/tso" alt="Total Service Organization">TSO</a></li><li><a href="//testonline/ec/vidconf" alt="Video Conferencing Services">Video Conferencing</a></li></ul></li></ul>';
$('#test-nav-wrapper').html(strGlobalNav);
//clean up the row of the mega menu. add css class to each element on bottom row.
//only if more than 7 elements. if more than 16, mm-3
jQuery('#test-nav-wrapper ul li ul').each(function(ulindex, ulele){
$total = jQuery(this).children('li').size();
if ($total <= 7) {
jQuery(this).addClass('mm-1');
}
else {
$cols = Math.floor(($total) / 8) + 1;
$remainder = $total % $cols;
$rows = Math.ceil($total / $cols);
jQuery(this).addClass('mm-' + $cols + ' total-' + $total + ' rem-'+$remainder );
jQuery(this).children().each(function(liindex, liele){
//alert("total: "+$total+", remainder: "+ $mod+", ulindex: "+ulindex+", liindex: "+liindex);
jQuery(this).addClass('col-' + Math.floor((liindex / $total * $cols)+1) );
if( (liindex+1) % $rows == 0) {
jQuery(this).addClass('last');
}
});
for (var colcount = 1; colcount<= $cols; colcount++){
jQuery(this).children('.col-'+colcount).wrapAll('<div class="col" />');
}
}
});
});更新:只要重读你的问题,注意到‘我的菜单是通过使用返回的javascrip的web服务来构建的’。
在由web服务生成的JS之后,在主体中执行jquery内容。在进行检查和添加类之前,按照下面的步骤将菜单添加到nav,从而启动on ready函数。
$(function() {
$('#test-nav-wrapper').html(strGlobalNav);
//clas checking stuff here
})对你来说应该挺好的。很高兴知道你是如何从web服务中获得菜单的。如果它是同一个域中的服务,请考虑使用$.ajax(),然后执行检查,并在成功函数中添加类。请参阅http://api.jquery.com/jQuery.ajax/
发布于 2013-10-11 16:34:36
在您的原始代码中,您忽略了jQuery(document).ready(function() 中的"{“标记--这就是为什么它不能在那里工作的原因!
jQuery(document).ready(function()
{
//clean up the row of the mega menu. add css class to each element on bottom row.
//only if more than 7 elements. if more than 16, mm-3
jQuery('#test-nav-wrapper ul li ul').each(function(ulindex, ulele){
$total = jQuery(this).children('li').size();
if ($total <= 7) {
jQuery(this).addClass('mm-1');
}
else {
$cols = Math.floor(($total) / 8) + 1;
$remainder = $total % $cols;
$rows = Math.ceil($total / $cols);
jQuery(this).addClass('mm-' + $cols + ' total-' + $total + ' rem-'+$remainder );
jQuery(this).children().each(function(liindex, liele){
//alert("total: "+$total+", remainder: "+ $mod+", ulindex: "+ulindex+", liindex: "+liindex);
jQuery(this).addClass('col-' + Math.floor((liindex / $total * $cols)+1) );
if( (liindex+1) % $rows == 0) {
jQuery(this).addClass('last');
}
});
for (var colcount = 1; colcount<= $cols; colcount++){
jQuery(this).children('.col-'+colcount).wrapAll('<div class="col" />');
}
}
}); });
小提琴
https://stackoverflow.com/questions/19322907
复制相似问题