开发者

Please explain this difference in cross browser string.length

I run this code in Firefox v6.0.2 and IE7. In Firefox, I select the radio button. Then click test. I get a string length of 10. In IE7, I get a string length of 9.

<script type="text/javascript">
    function TestMethod() {

        var name;
        var address;
        var city;
        var state;
        var zip;

        var indexor = 0;
        $('input[name=radioBtnSet1]:checked').parent().siblings().each(function (i, cell) {
            if (indexor === 0)
                name = $(cell).text();
            else if (indexor === 1)
                address = $(cell).text();
            else if (indexor === 2)
                city = $(cell).text();
            else if (indexor === 3)
                state = $(cell).text();
            else if (indexor === 4)
                zip = $(cell).text();

            indexor++;
        });

        alert(name.length);
        alert('FACILITY NAME: ' + '|' + name + '|');
    }
</script>

<input id="runTest" onclick="javascript:TestMethod();" type="button" value="Test"/>

<table id="someTable">
<thead>
    <tr>
        <th></th>
        <th>Header</th>
        <th cla开发者_开发问答ss="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th>Date</th>
    </tr>
</thead>
<tbody>
<tr>
    <td><input type="radio" value=" HHH VALUE" name="radioBtnSet1" /></td>
    <td style="text-align: left;"> HHH VALUE</td><td class="DisplayNone">200 SOME STREET DR</td>
    <td class="DisplayNone">CITY</td><td class="DisplayNone">TX</td>
    <td class="DisplayNone">75007-3726</td>
    <td style="padding-left: 1em;">9/30/2011</td>
</tr>
</tbody>
<tfoot>
    <tr>
        <th></th>
        <th></th>
        <th class="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th class="DisplayNone"></th>
        <th></th>
    </tr>
</tfoot>
</table>

Why? How can I get these to be equivalent?


As it can be seen in the comments in this page, the problem is with jQuery's text function. In IE 7 it doesn't preserve the leading and trailing white spaces. In FF, it does. Hence the different strings and different lengths in IE 7 and FF.

If you need the whitespace, try using &nbsp; instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜