javascript按钮没有显示

时间:2013-01-29 15:50:07

标签: javascript html

好吧,我有这个代码(我从保罗爱尔兰页面+一些修改)(它给你你的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>

事情是它没有显示测试按钮

2 个答案:

答案 0 :(得分: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>

N.B。我包含了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}}})();

答案 1 :(得分:0)

但是如果你想把它保存在脚本标签中,你需要像这样修改你的代码:

<script type="text/javascript"> 
   document.write('<button type="button" onclick="test()">testing</button>');
</script>