我使用JS在XML中搜索并获取它的元素,以便在进一步的处理中使用它,我使用堆箱来显示元素,然后使用.selected来获取选定的元素,以显示它的子元素。
这里的问题是,为了在获得子元素之前进行一些名称处理,我希望获得所选的元素,但是当我将其存储在var中时没有得到任何信息,直接使用完整的查询时,
为了前夫,
var section = '', text = '' ;
section = $('menu').find($(".selected").text()).attr('nodeName');
alert($('menu').find($(".selected").text()).attr('nodeName'));在该代码中,section等于nothing,text等于nothing,警告 undefined,而在处理过程中直接使用$('menu').find($(".selected").text()).attr('nodeName')非常有效。
更新:
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<menu parent_id="0" >
<Menu cat="main">
</Menu>
<Soup cat="main">
<Mushrom0Tomato.Soap price = "15.95" hasoption = "false">
</Mushrom0Tomato.Soap>
<Cream.Of.Chicken.Soap price = "16.95" hasoption = "true">
<Add.Mushroom price = "3.95"></Add.Mushroom>
<none price = "0"></none>
</Cream.Of.Chicken.Soap>
<Season.Soap price = "15.95" hasoption = "false">
</Season.Soap>
</Soup>
</menu>JS代码:
var cats=[], cati=0, total=0, selectedarray=[], itemoptions=[], optionstring='', section='', si=0, oi=0, items=[];
$("#total").append("Total: " + total + " EGP");
$('#dummy').load('cafe.xml',function() {
initialize();
})
function initialize(){
ct=$('menu').children().length;
for(cati==0;cati<=ct-1;cati++)
{
cats[cati]=$('menu').children().eq(cati).prop('nodeName');
var realname = cats[cati];
if(realname.indexOf(".") != -1){
realname = realname.replace(/\./g,' ');
}
$('.basic-example').append('<option value="option1">'+realname+'</option>');
}
$(".basic-example").heapbox({'onChange':function(){loadmenu()},effect:{type:"fade",speed:"slow"}});
}
// loading the items to the menu
function loadmenu()
{
$("#gallery").empty();
section = $('menu').find($(".selected").text()).attr('nodeName');
alert($("menu .selected").text().toString());
.
.
.发布于 2013-12-16 08:28:52
.find用于查找所选节点的子元素。
你可能只是想
$("menu .selected").text();和
$("menu .selected").attr("nodeName");与此类似的.find (虽然可能是不必要的)将是
$("menu").find(".selected").text();和
$("menu").find(".selected").attr("nodeName");https://stackoverflow.com/questions/20606308
复制相似问题