使用Javascript隐藏某些xml

时间:2013-08-28 18:53:37

标签: javascript xml html5

无论如何要显示一些li标签,同时使用Javascript隐藏一些?如果我的问题没有意义,我很抱歉。

让我在这里写下我的代码。

XML:

    <array>
    <words>
    <name>Milk</name>
    <background>Background of Milk</background>
    <recipes>Recipes using Milk</recipes>
    </words>

    <words>
    <name>Tree</name>
    <background>Background of Tree</background>
    <recipes>NIL</recipes> or Not Applicable
    </words>
    </array>

脚本/使用Javascript:

    <script>
    var wordsName, wordsBG, wordsRecipes, wordsCurrent;
    var wordsArray = new Array();
    $.ajax({
    type:"GET",
    url:"words.xml",
    dataType:"xml",
    success:function(xml)
        {
            $(xml).find('words').each(function() { //find <words> in words.xml
            wordName = $(this).find('name').text();
            wordBG = $(this).find('background').text();
            wordRecipes = $(this).find('recipes').text();
        $('<li><a href="#description">'+threatsName+'</a></li>').appendTo("#testingList");
            threatsArray.push({threatsName:threatsName, threatsBG:threatsBG, threatsCases:threatsCases});
        })
            $('#threats li').click(function(){ //li

                wordsCurrent = $(this).index()
                $('#description h1').text(threatsArray[wordsCurrent].threatsName);
                $('#name').text(threatsArray[wordsCurrent].threatsName);

                var backgroundInformation = wordsArray[wordsCurrent].wordsBG;
                backgroundInformation = backgroundInformation.split("\\n"); //to clear the line
                $('#backgroundInfo').empty(); //empty the things inside
                for (x in backgroundInformation) { //starts from zero and go to the last object
                    $('#backgroundInfo').append(backgroundInformation[x]+ '<br />');
                } //for loop CLOSE 

                var recipesInformation = wordsArray[wordsCurrent].wordsRecipes;
                recipesInformation = recipesInformation("\\n"); //to clear the line
                $('#recipesInfo').empty(); //empty the things inside
                for (x in recipesInformation) { //starts from zero and go to the last object
                    $('#recipesInfo').append(recipesInformation[x]+ '<br />');
                } //for loop CLOSE 
            })

        }
    </script>

最后,我的HTML

<div data-role="page" id="menu">
<div data-role="header">
<h1>Home Page</h1>
</div>

<div data-role="content">
  <ul data-role="listview">
    <li>
        <a href="#words" data-transition="pop"><h3>Words</h3></a>
    </li>
    <li>
        <a href="#notAvail" data-transition="pop"><h3>Not Available</h3></a> <!--Not Available-->
    </li>
  </ul>
</div>

                            

    </ul>
</div>

                                           

    <div data-role="content"> <!--Start of DESCRIPTION content-->
        <ul data-role="listview" id="informationList">
            <li>
                <a href="#background" data-transition="pop"><h3>Background</h3></a>
            </li>
            <li>
                <a href="#recipes" data-transition="pop"><h3>Recipes</h3></a>
            </li>
        </ul>
    </div> <!--End of DESCRIPTION content-->

</div>  <!--End of DESCRIPTION-->

<div data-role="page" id="background"> <!--Start of BACKGROUND-->
    <div data-role="header">
        <a href="#description" data-transition="slide" data-icon="back" data-iconpos="notext" data-direction="reverse"></a>
        <h1>Background</h1>
        <a href="#menu" data-transition="slide" data-icon="home" data-iconpos="notext" data-direction="reverse"></a>
    </div>

    <div data-role="content"> <!--Start of BACKGROUND content-->
        <p id="backgroundInfo">Not Available</p>
    </div> <!--End of BACKGROUND content-->

</div>  <!--End of BACKGROUND-->

<div data-role="page" id="recipes"> <!--Start of RECIPES-->
    <div data-role="header">
        <a href="#description" data-transition="slide" data-icon="back" data-iconpos="notext" data-direction="reverse"></a>
        <h1>Background</h1>
        <a href="#menu" data-transition="slide" data-icon="home" data-iconpos="notext" data-direction="reverse"></a>
    </div>

    <div data-role="content"> <!--Start of RECIPES content-->
        <p id="recipesInfo"> Recipes</p>
    </div> <!--End of RECIPES content-->

</div>  <!--End of RECIPES-->

如上所述,Array,Tree中的第二个元素没有配方选项卡的任何信息,如何在用户点击树时提示页面,配方li会隐藏自己?

非常感谢。这是我第一次发送问题。

1 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow!您似乎正在解析XML并使用JavaScript动态生成HTML。在您的情况下,最简单的方法是在解析过程中检测条件,并为&lt; li&gt;添加“class”属性。节点。然后在CSS中的某个地方定义样式类,如下所示:

<li class="no-recipe">blah blah blah</li>

<style>
    li.no-recipe {
        display:none;
    }
</style>

不确定您的目标是什么,但声明式方法几乎总是比动态方法更好(使用JavaScript)。