1

好的,我有这段代码(我从 Paul irish 页面获得+一些修改)(它为您提供了您的 ie 版本(标准内容):

 <!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
 <!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
 <!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
 <!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
 <!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
</head>
<body>
<h1>test:</h1>
<script type="text/javascript"> 
$(function test()) {
"use strict";
// Detecting IE
var oldIE
if ($('html').is('.ie6, .ie7, .ie8')) {
    oldIE = true;
}
if (oldIE) == true {
document.write("version<ie9")

} else {
   document.write(" version => ie9")

}
}
</script>
<script type="text/javascript"> 
<button type="button" onclick="test()">testing</button>
</script>
</body>

 </html>

问题是它没有显示测试按钮

4

2 回答 2

4

<button>元素不应被标签<script>包围。毕竟,它是 HTML,而不是 JavaScript。

<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>
<title>A HTML document needs a title to be valid.</title>
</head>
<body>
    <button id="test">I feel so clickable today.</button>

<!-- You must include jQuery before you can use its methods -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
"use strict";

    (function ($) {
        // Assignment with the ternary operator
        var oldIE = ($('html').is('.ie6, .ie7, .ie8')) ? true : false;

        // You need at least jQuery 1.7 to use .on()
        $('#test').on('click', function() {
            console.log(oldIE);
        });
    }(jQuery));

</script>
</body>
</html>

注意我包含 jQuery 是因为我$在您的代码中看到了一个符号。


由于您应该在开发过程中使用控制台,因此这里有一个片段可以防止在不提供控制台的浏览器中出现 JS 错误。

// Avoid `console` errors in browsers that lack a console
(function(){var e;var t=function(){};var n=["assert","clear","count","debug","dir","dirxml","error","exception","group","groupCollapsed","groupEnd","info","log","markTimeline","profile","profileEnd","table","time","timeEnd","timeStamp","trace","warn"];var r=n.length;var i=window.console=window.console||{};while(r--){e=n[r];if(!i[e]){i[e]=t}}})();
于 2013-01-29T15:52:36.760 回答
0

但是,如果您想将其保留在脚本标记中,则需要像这样修改代码:

<script type="text/javascript"> 
   document.write('<button type="button" onclick="test()">testing</button>');
</script>
于 2013-01-29T16:04:15.257 回答