【发布时间】:2011-10-27 03:24:57
【问题描述】:
附加到我从切片器收到的模板中,有一个用于为菜单项着色的 javascript 文件。 现在我需要将模板转换为 Drupal 模板。 javascript基于每个菜单项都有一个id,根据项目的顺序。 在 Drupal 中,菜单项只有类(menu-341、menu-342 等)。我知道我可以调整 javascript,因此它按类获取菜单项,但实际上是不可能的,因为我需要完全打乱整个文件,这是我试图阻止的。
那么我可以为我的所有主菜单项添加一个 ID,例如“menu-item-1”、“menu-item-2”等吗? 我应该在template.tpl.php中这样做,还是直接更改page.tpl.php中的输出?
感谢您的帮助。
编辑: 我很绝望,因为我只是不知道如何解决这个问题。还有其他方法可以为我的菜单项着色吗? 我正在使用的脚本在它们的升序 id (custom-menu-item-id-x) 上选择菜单项,并从数组中为它们提供一个背景,该背景与 ID 的“x”相关联。四种颜色:第 1 项的颜色 1、第 2 项的颜色 2、第 5 项的颜色 1 等..... 没有其他方法可以解决这个问题吗?我不能让 jQuery 按升序自动选择它们吗(它遇到的第一个项目获得第一个背景,第二个项目获得第二个背景等)?请提出想法。在我看来,我什么都试过了。
这是我当前的脚本:
//Sequence of the background colors from the menu items, from left to right.
var backgrounds = ["blue", "green", "pink", "purple"];
//Gives the menu items a style when hovering over it, in the sequence of the setting in the variable 'backgrounds', on top.
jQuery(document).ready(function MenuColor($){
for (var i = 0; i <= 10 ; i++) {
document.getElementById("custom-menu-item-id-" + (i + 1)).onmouseover = (function(valueOfI){ return function() {
this.style.background = "url('images/" + backgrounds[(valueOfI % backgrounds.length)] + ".jpg')";
}; })(i);
var ActiveLi = $('.active').attr('id');
if("custom-menu-item-id-" + (i + 1) != ActiveLi) {
document.getElementById("custom-menu-item-id-" + (i + 1)).onmouseout = function() {
this.style.background = 'none';
}
}
}
});
【问题讨论】: