0

该程序现在读取 xml 代码,获取股票缩写,按字母顺序对它们进行排序,然后将它们打印到 uo 列表中。如果将鼠标悬停在缩写上,颜色将变为红色。我的目标是当您将鼠标悬停在缩写上时,它将显示该公司的 xml 数据中的所有数据。我尝试使用 if 语句来说明符号(xml 文件中的缩写)是否等同于名称(数组中的缩写),然后它会为它打印出所有垃圾。打印所有内容的行以我想要的格式正常工作。我只需要处理 if 语句。

我发现我无法将两个变量与 == 进行比较。请记住,符号也是一个属性,名称来自存储符号的数组。我也试着说 - if(checkPassword(name, symbol)) - 并像我在下面的 jQuery 代码中所做的那样将其全部打印出来,但这不起作用。

我在我正在处理的 if 语句旁边放了一条评论,它位于 jQuery 的底部。

HTML:

 <body onload="onBodyLoad()">
  <div id="stockList"></div>
  <br />
  <br />
  <br />
  <div id="stockInfo"></div>

jQuery:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "stocks.xml",
        dataType: "xml",
        success: function (xml) {
            var companyNames = [];
            $(xml).find('Stock').each(function () {
                var symbol = $(this).attr('symbol');
                companyNames.push(symbol);
            });



            companyNames.sort();
            $.each(companyNames, function (index, name) {
                $('#stockList').append('<div><li>' + name + '</li></div>');

            });


            function CheckPassword(val, val2) {

                var strInput = val.value;
                var strInput2 = val2.value;

                if (strInput != strInput2) {
                    val2.focus();
                    val2.select();
                    return false;
                } else
                    return true;

            }


            $(xml).find('Stock').each(function () {
                var company = $(this).find('Company').text();
                var symbol = $(this).attr('symbol');                  
                var market = $(this).find('Market').text();
                var sector = $(this).find('Sector').text();
                var price = $(this).find('Price').text();

                var low = $(this).find('Low').text();
                var high = $(this).find('High').text();


                var amount = $(this).find('Amount').text();
                var yieldx = $(this).find('Yield').text();
                var frequency = $(this).find('Frequency').text();

                $('*').mouseover(function () {
                    $('#stockList li').text($(this).attr('comparison'));
                });


                $('#stockList li').hover(
                function () {
                    $(this).css({ color: 'red' }); //mouseover
                    if (name == symbol) {  // THIS IS THE STATEMENT YOU'RE LOOKING FOR PROGRAMMING GODS

                            $('#stockInfo').append('<div><ol><li>' + "Company = " + company + '</li><br/><li>' + "Market = " + market + '</li><br/><li>' + "Sector = " + sector + '</li><br/><li>' + "Price = " + price + '</li><br/><li>' + "Year Range = " + low + " " + high + '</li></ol><br/>');
                        }

                },
                function () {
                    $(this).css({ color: 'navy' }); // mouseout
                    $('#stockInfo').empty();
                }



            );

            });




        }
    });
});

XML 示例:

<Products>
<Stock symbol="GOOG">
    <Company>Google</Company>
    <Market>NASDAQ</Market>
    <Sector>Software</Sector>
    <Price>$487.80</Price>
    <YearRange>
        <Low>$331.55</Low>
        <High>$488.50</High>
    </YearRange>
    <Dividend available="false"/>
</Stock>
<Stock symbol="BA">
    <Company>Boeing Company</Company>
    <Market>NYSE</Market>
    <Sector>Aerospace</Sector>
    <Price>$79.05</Price>
    <YearRange>
        <Low>$63.70</Low>
        <High>$89.58</High>
    </YearRange>
    <Dividend available="true">
        <Amount>$1.20</Amount>
        <Yield>$1.50</Yield>
        <Frequency>QTR</Frequency>
    </Dividend>
</Stock>
<Stock symbol="MO">
    <Company>Altria Group</Company>
    <Market>NYSE</Market>
    <Sector>Comsumables</Sector>
    <Price>$81.70</Price>
    <YearRange>
        <Low>$68.36</Low>
        <High>$85.00</High>
    </YearRange>
    <Dividend available="true">
        <Amount>$3.44</Amount>
        <Yield>$4.2</Yield>
        <Frequency>ANNUAL</Frequency>
    </Dividend>
</Stock>
</Products>
4

1 回答 1

0
        var companyData = [];
        $(xml).find('Stock').each(function () {
            var symbol = $(this).attr('symbol');
            companyNames.push(symbol);
            companyData[symbol] = {
                company: $(this).find('Company').text(),
                symbol: $(this).attr('symbol'),                
                market: $(this).find('Market').text(),
                sector: $(this).find('Sector').text(),
                price: $(this).find('Price').text(),
                low: $(this).find('Low').text(),
                high: $(this).find('High').text(),
                amount: $(this).find('Amount').text(),
                yieldx: $(this).find('Yield').text(),
                frequency: $(this).find('Frequency').text()
            };
        });

        ...

        $("#stocklist li").hover(function() {
            $(this).css({ color: 'red' }); //mouseover
            var info = companyData[$(this).text()];
            $('#stockInfo').append('<div><ol><li>' + "Company = " + info.company + '</li><br/><li>' + "Market = " + info.market + '</li><br/><li>' + "Sector = " + info.sector + '</li><br/><li>' + "Price = " + info.price + '</li><br/><li>' + "Year Range = " + info.low + " " + info.high + '</li></ol><br/>');
        });
于 2013-03-29T21:14:00.920 回答