【问题标题】:My JQuery code is not working in Internet Explorer while working on other Browser like Mozilla and Chrome在 Mozilla 和 Chrome 等其他浏览器上工作时,我的 JQuery 代码无法在 Internet Explorer 中工作
【发布时间】:2014-04-22 19:56:58
【问题描述】:

在这段代码中,JQuery 代码在 Firefox 和 Chrome 上运行时无法在 Internet Explorer 中运行
//alert($('input[type="text"]').eq(0).val()); 为什么这里没有显示此提醒?

<html>
<head>
    <style>
        div {
            border: 2px solid blue;
        }
        .a {
            color: red
        }
        .b {
            background-color: green;
        }
        .c {
            font-size: 55px;
        }
    </style>
</head>

<body>
    <div id="container">this is my div
        <input type="text" value="50" name="first_input">first label</input>
        <button>first button</button>
        <input type="text">second label</input>
        <input type="text">third label</input>
        <input type="text">fourth label</input>
        <button>second button</button>
        <input type="text">fifth label</input>
        <button>third button</button>
        <input type="text">sixth label</input>
        <button>fourth button</button>
        <input type="text">seventh label</input>
        <button>fifth button</button>
        <button>sixth button</button>
        <button id="ss">seventh button</button>
        <label id="lbl1">hello</label>
    </div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('input:text').eq(0).attr({
                title: 'please input the value',
                placeholder: 'insert a value',
                name: 'input_type',
                //value:25
            });
            $('#ss').click(function () {
                <!--  alert($('input[type="text"]').get(0).value);  this is not work on I.Explorer  -->
                <!--   alert($('input[type="text"]').get(0).name);  this is not work on I.Explorer  -->
                <!--   alert($('input[type="text"]').eq(0).val());    -->
                alert($('input[type="text"]').eq(0).val()); <!-- this is not work on I.Explorer  -->
            });
        });
    </script>
</body>
</html>

【问题讨论】:

  • name:'input_type',
  • 请到jsfiddle.net 做个小技巧,发到这里,我们可以帮助你:)
  • @DanielCheung 上面有足够的代码不需要 jsfiddle。人们太快要求这个了。
  • @SalmanA 一针见血。在大多数浏览器中,您可以在对象构造函数中使用尾随逗号,但 IE 则不行。它倒下,之后停止所有代码执行。
  • 解决方案 HERE使用短结束标签,这意味着使用 &lt;input&gt;..&lt;/input&gt; 而不是 &lt;input .../&gt;

标签: jquery html css


【解决方案1】:
$('input:text').eq(0).attr({
                title: 'please input the value',
                placeholder: 'insert a value',
                name: 'input_type', <- ERROR HERE
                //value:25
            });

不要在最后一个属性中添加逗号(,)。 IE 不支持这个。 所以删除那个逗号并尝试我认为它会正常工作.. 一切顺利。

也改变你的输入语法。

正确的语法是:&lt;input *attributelist* /&gt;

例如

 <input type="text" value="50" name="first_input" />

-这是一个很好的编码习惯。

更新

<div id="container">this is my div
    <input type="text" value="50" name="first_input" />
        <button>first button</button>
        <input type="text" />second label
        <input type="text" />third label
        <input type="text" />fourth label
        <button>second button</button>
        <input type="text" />fifth label
        <button>third button</button>
        <input type="text" />sixth label
        <button>fourth button</button>
        <input type="text" />seventh label
        <button>fifth button</button>
        <button>sixth button</button>
        <button id="ss">seventh button</button>
        <label id="lbl1">hello</label>
    </div>
$(document).ready(function(){
    $('input:text').eq(0).attr({
                    title: 'please input the value',
                    placeholder: 'insert a value',
                    name: 'input_type',
                    value:25
                });
    $('#ss').click(function () {
                   alert($('input[type="text"]').get(0).value);  
                });
});

FIDDLE

【讨论】:

  • 您能否将其标记为正确答案。 @chetantyagi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多