[英]Setting different <span> tags for li and ul li (nested)
I really need to nail this... I've been at it way too long now. 我真的需要钉这个……我已经来太久了。
I think I need to get the innerText of each <a>
element and put it into li_text variable. 我想我需要获取每个
<a>
元素的innerText并将其放入li_text变量中。
The following is the 'supplied' code for this theme. 以下是此主题的“提供”代码。 The theme does not support submenus.
主题不支持子菜单。 BUT I need this so I'm trying to make it work with submenus.
但是我需要这个,所以我试图使其与子菜单一起使用。
The following js script is partly 'building' the HTML. 以下js脚本部分地“构建”了HTML。
var$ $main_menu = $('#main_menu');
$main_menu.find('li').each( function(){
var $this_li = $(this),
li_text = $this_li.find('a').html();
$this_li.find('a').html( '<span class="main_text">' + li_text + '</span>' + '<span class="menu_slide">' + li_text + '</span>' )
} );
It seems that it's this piece of code: li_text = $this_li.find('a').html(); 似乎就是这段代码:li_text = $ this_li.find('a')。html(); from the above.
从上面。 that needs to tweaking.... I need that line to take the 'a' from it's closest parent above..... does this make sense?
需要调整...。我需要该行从上面最接近的父级中获取“ a” .....这有意义吗?
together with this (from header.php) 与此一起(来自header.php)
$primaryNav = '';
if (function_exists('wp_nav_menu')) {
$primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => $menuID, 'echo' => false ) );}
The navigation HTML comes out like this: 导航HTML如下所示:
<nav id="main_menu" style="opacity: 1; display: none;">
<ul id="menu-dk-fl370" class="clearfix">
<li id="menu-item-23" class="mcdu_left menu-item menu-item-type-post_type menu-item-object-page menu-item-23 even">
<li id="menu-item-48" class="mcdu_right menu-item menu-item-type-post_type menu-item-object-page menu-item-48 odd">
<li id="menu-item-27" class="nolink menu-item menu-item-type-custom menu-item-object-custom menu-item-27 even">
<li id="menu-item-55" class="has_sub mcdu_right menu-item menu-item-type-post_type menu-item-object-page menu-item-55 odd">
<a href="http://fl370.com/?page_id=53">
<span class="main_text">MEDIA</span>
<span class="menu_slide">MEDIA</span>
</a>
<ul id="sub-menu" class="sub-menu" style="display: none;">
***<li id="menu-item-67" class="mcdu_sub mcdu_right menu-item menu-item-type-post_type menu-item-object-page menu-item-67">
<a href="http://fl370.com/?page_id=60">
<span class="main_text">
<span class="main_text">MEDIA</span>
<span class="menu_slide">MEDIA</span>
</span>
<span class="menu_slide">
<span class="main_text">MEDIA</span>
<span class="menu_slide">MEDIA</span>
</span>
</a>
</li>***
<li id="menu-item-66" class="mcdu_sub mcdu_right menu-item menu-item-type-post_type menu-item-object-page menu-item-66">
</ul>
</li>
</ul>
</nav>
and so on... 等等...
** **
THIS IS WHAT IM TRYING TO GET.... 这就是我想要得到的...。
<span class="main_text">
<span class="main_text">BILLEDER</span>
<span class="menu_slide">BILLEDER</span>
</span>
<span class="menu_slide">
<span class="main_text">VIDEO</span>
<span class="menu_slide">VIDEO</span>
</span>
These 'names' is inserted via the li_text function in js. 这些“名称”是通过js中的li_text函数插入的。 But it seems it's inserting only the first li a name.
但似乎只在第一个li插入名称。
I need the script to take the page name from its 'own'/parent li a. 我需要脚本来从其“所有者” /父级li a获取页面名称。 AND still apply the js script....
并且仍然应用js脚本...。
** **
Hope someone can help.... 希望有人可以帮助...。
Thanks 谢谢
Personally, I hate using html()
to populate snippets of HTML. 我个人不喜欢使用
html()
来填充HTML片段。 I much rather prefer creating the elements, and appending them to the DOM. 我更喜欢创建元素,并将其附加到DOM。 You end up with more control and understanding of what you're doing.
您最终将获得更多控制权并了解自己在做什么。
$("#main_menu li").each(
function(i, element)
{
$("<a />").attr("href", "some link here").appendTo(element);
$("<span />").addClass("class name here").text("some text here").appendTo(element);
// ...etc.
}
);
(If you post a little more about what code you're expecting in the end, I can give you more specifics about what I mean here...) (如果您最终发表了更多关于期望的代码的信息,那么我可以在这里提供更多关于我的意思的详细信息...)
That aside, you're probably having extra issues because your HTML is malformed. 除此之外,由于HTML格式不正确,您可能还会遇到其他问题。 You close your
<a>
tag twice, and you don't close your <li>
tag. 关闭两次
<a>
标签,但不关闭<li>
标签。 Also, you don't close a <span>
tag, because one of your angle brackets is reversed ( >/span>
). 另外,您也不要关闭
<span>
标记,因为其中一个尖括号被反转了( >/span>
)。 jQuery is going to have a tough time dissecting the HTML elements if they're not done properly. 如果HTML元素处理不当,jQuery将很难剖析它们。
If your code structure stays as it is then you can grab that content by doing this. 如果您的代码结构保持不变,则可以通过执行此操作来获取该内容。 I am assuming you can't use the class names or IDs
我假设您不能使用类名或ID
$main_menu.find('> ul > ul').html();
or 要么
$('#main_menu > ul > ul').html();
this will return everything inside of the ul but not the ul itself, so you need to create it and add it. 这将返回ul内部的所有内容,但不会返回ul本身,因此您需要创建并添加它。
var container_ul = '<ul class="' + $main_menu.find('> ul > ul').attr("class") + '" id="' + $main_menu.find('> ul > ul').attr("id") + '">';
define container_ul outside of the loop, so the final thing would look something like this 在循环之外定义container_ul,所以最终的结果看起来像这样
var $main_menu = $('#main_menu');
var container_ul = '';
var final_content = '';
$main_menu.find('> ul > ul').each( function(){
container_ul = '<ul class="' + $(this).attr("class") + '" id="' + $(this).attr("id") + '">';
li_text = $(this).html();
final_content += container_ul + li_text + '</ul>';
});
so final_content will have the block of code you want, from here you can decide what to do with that block of code. 因此final_content将具有您想要的代码块,从这里您可以决定如何处理该代码块。 Probably not the prettiest way to get the content but it should work
可能不是获取内容的最漂亮方法,但它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.