我想应用此属性:
onclick="expandMenu(this.parentNode)"
与class =“parent”的每个li
到目前为止,我有这个并且无法让它工作:
<script type="text/javascript">
if ($('li').hasClass('parent'))
{
'onclick' => 'expandMenu(this.parentNode)';
}
</script>
这是Magento生成的菜单的浏览器端代码。每个li里面都有一个锚标签。我想我需要让那些无法点击才能使它工作。我希望能够做的是通过点击li来扩展产品类别(如箭头所示),而不是让它带你到页面:
<div id="sidebar-nav" class="sidebar-nav-left">
<div class="block-title">
<strong><span>Categories</span></strong>
</div>
<div class="block-content">
<ul id="sidebar-nav-menu">
<li class="level0 nav-1 first parent" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 10px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="tion.html"><span class="category_name">tion</span></a>
</div>
<ul class="level0" style="margin-left: 5px; padding-left: 10px;" expanded="0">
<li class="level1 nav-1-1 first parent" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 10px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="system.html"><span class="category_name">System</span></a>
</div>
<ul class="level1" style="margin-left: 5px; padding-left: 10px;" expanded="0">
<li class="level2 nav-1-1-1 first last" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
a href="accessories.html"><span class="category_name">Accessories</span></a>
</div>
</li>
</ul>
</li><li class="level1 nav-1-2" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="pack.html"><span class="category_name">Pack</span></a>
</div>
</li><li class="level1 nav-1-3" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="system.html"><span class="category_name">System</span></a>
</div>
</li><li class="level1 nav-1-4 parent" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 10px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="accessories.html"><span class="category_name"> Accessories</span></a>
</div>
<ul class="level1" style="margin-left: 5px; padding-left: 10px;" expanded="0">
<li class="level2 nav-1-4-2 first" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="needles.html"><span class="category_name"> Needles</span></a>
</div>
</li><li class="level2 nav-1-4-3" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="cha.html"><span class="category_name">Percha</span></a>
</div>
</li><li class="level2 nav-1-4-4" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="condensers.html"><span class="category_name">Condensers</span></a>
</div>
</li><li class="level2 nav-1-4-5" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="resilon.html"><span class="category_name">Resinate</span></a>
</div>
</li><li class="level2 nav-1-4-6 last" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="pluggers.html"><span class="category_name">Heat</span></a>
</div>
</li>
</ul>
</li><li class="level1 nav-1-5" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="heat.html"><span class="category_name">Heat</span></a>
</div>
</li><li class="level1 nav-1-6 last" style="margin-left: 0px;">
<span class="arrow" onClick="expandMenu(this.parentNode)"
style="width: 8px; height: 0px;"></span>
<div class="collapsible-wrapper" style="margin-left: 14px;">
<a href="replacement-parts.html"><span class="category_name">Parts</span></a>
</div>
</li>
</ul>
</li> </ul>
</div>
</div>
答案 0 :(得分:4)
只需执行以下操作
$("li.parent").click(function()
{
expandMenu(this.parentNode);
});
示例here
以下是我认为您正在寻找的更新版本。
它将检测链接点击并检查其父li
以查找课程parent
。如果找到该类,它将阻止链接并展开菜单。如果没有找到该类,则链接的工作原理应该
$("#sidebar-nav-menu a").click(function()
{
if($(this).parent().parent().hasClass("parent"))
{
expandMenu(($(this).parent()).parentNode);
//return flase prevents the anchor link from working
return false;
}
});
修改强>
您没有告诉我们您已将jquery $
重新定义为jQuery
将此内容放入unclick.js
jQuery(document).ready( function()
{
jQuery("#sidebar-nav-menu li.level0 > div > a").click(function()
{
if(jQuery(this).parent().parent().hasClass("parent"))
{
expandMenu(this.parentNode.parentNode);
//return flase prevents the anchor link from working
return false;
}
});
});
答案 1 :(得分:2)
这适用于文档中li
类目前的所有parent
元素:
$('li.parent').on('click', function(){
expandMenu(this.parentNode);
});
但是,如果您想对以后可能添加的 future 元素执行此操作,可以使用以下命令:
$(document.body).on('click', 'li.parent', function(){
expandMenu(this.parentNode);
});
修改的
就防止链接被触发而言,您可以使用以下代码:
$('#sidebar-nav-menu a').on('click', function(e) {
e.preventDefault();
});
但这会阻止菜单上所有的链接被触发(除非调用此方法后链接已添加);
再次编辑
最后,我们可以使用它,它只会触发不具有子菜单的链接。
$('#sidebar-nav-menu a').on('click', function(e) {
if($(this).parent().siblings('ul').length)
{
// If my anchor is associated with a sub-catagory, prevent it from firing.
e.preventDefault();
}
expandMenu(this.parentNode);
});
答案 2 :(得分:1)
如果你想使用jQuery,你所要做的就是在这个文件中添加以下代码: [your_magento_root] /skin/frontend/default/default/js/sidenavcollapse.js
jQuery(document).ready( function()
{
$('#sidebar-nav-menu div.collapsible-wrapper').click(function(){
expandMenu(this.parentNode);
return false;
});
});
不要忘记加载jQuery库。如果您喜欢以其他方式执行此操作,例如编辑生成的HTML,请告诉我,我会说的。
修改强>
我们返回false以停用“a”标签的“href”。
答案 3 :(得分:0)
不要使用该属性。使用jQuery click函数:
$('li.parent').click(function(){
expandMenu(this.parentNode);
});