Hi guys this is my jQuery part that makes my menu for my pages.
大家好,这是我的jQuery部分为我的页面制作菜单。
function fetchmenus() {
$.getJSON('sys/classes/fetch.php?proccess=1', function(status) {
// get line status
$.each(status, function(i, item) {
if (item.id == "1") {
active = "class='active'";
lastpageid = item.href;
}
else {
active = "class='nonactive'";
}
$('<li id="' + item.href + '" ' + active + '><a href="#' + item.href + '">' + item.menuTitle + '<span class="menuspan" id="' + item.href + '"></span></a></li>').appendTo('#menuarea ul#mainmenu');
});
});
}
What i want to do is get the item.menuTitle
in the <a
but before the <span>
我要做的是得到这个项目。menuTitle在 之前 ,但在
Currently i do it this way:
目前我是这样做的:
$('ul#mainmenu li').live('click', function(event) {
//alert(this.id);
$("li#" + lastpageid).removeClass();
fetchpage(this.id);
$("#largemenutop").html($(this).text());
$("li#" + this.id).addClass("active");
lastpageid = this.id;
});;
is there a better way to do this?
有更好的方法吗?
2 个解决方案
#1
8
Yes, you can select only the text contents of the element, like this:
是的,您只能选择元素的文本内容,如下所示:
var text = '';
$('a').contents().each(function(){
if(this.nodeType === 3){
text += this.wholeText;
}
});
$("#largemenutop").html(text);
#2
22
Nice solution Herman, though it can be reduced to something like this:
不错的解决方案,赫尔曼,虽然它可以简化成这样:
JS
$('li a').contents().filter(function() {
return this.nodeType == 3;
}).text();
HTML
<li><a href="#">Apple<span>hi</span> Juice</a></li>
Will return Apple Juice
将返回苹果汁
Fiddle: http://jsfiddle.net/49sHa/1/
小提琴:http://jsfiddle.net/49sHa/1/
#1
8
Yes, you can select only the text contents of the element, like this:
是的,您只能选择元素的文本内容,如下所示:
var text = '';
$('a').contents().each(function(){
if(this.nodeType === 3){
text += this.wholeText;
}
});
$("#largemenutop").html(text);
#2
22
Nice solution Herman, though it can be reduced to something like this:
不错的解决方案,赫尔曼,虽然它可以简化成这样:
JS
$('li a').contents().filter(function() {
return this.nodeType == 3;
}).text();
HTML
<li><a href="#">Apple<span>hi</span> Juice</a></li>
Will return Apple Juice
将返回苹果汁
Fiddle: http://jsfiddle.net/49sHa/1/
小提琴:http://jsfiddle.net/49sHa/1/