jquery,why.data()在internetexplorer中不起作用
jquery, why .data() is not working in internet explorer?
有没有办法让data()在internet explorer中工作,例如,如果你在Chrome或Firefox中尝试这个例子,它会工作,但在ie:中不行
<!doctype html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>A div</div>
<button>Get "blah" from the div</button>
<button>Set "blah" to "hello"</button>
<button>Set "blah" to 86</button>
<button>Remove "blah" from the div</button>
<p>The "blah" value of this div is <span>?</span></p>
<script>
$( "button" ).click(function() {
var value;
switch ( $( "button" ).index( this ) ) {
case 0 :
value = $( "div" ).data( "blah" );
break;
case 1 :
$( "div" ).data( "blah", "hello" );
value = "Stored!";
break;
case 2 :
$( "div" ).data( "blah", 86 );
value = "Stored!";
break;
case 3 :
$( "div" ).removeData( "blah" );
value = "Removed!";
break;
}
$( "span" ).text( "" + value );
});
</script>
</body>
</html>
这是中的最后一个示例:http://api.jquery.com/data/
您的HTML和JS代码是可以的。只有我想将JS代码包装到中
$(function(){
// js code
});
我添加了几个alert(),返回的结果与预期的一样
fiddle 的工作示例
这里完全是HTML/JS代码。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Jquery data - jsFiddle demo by User86745458</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$( "button" ).click(function() {
var value;
switch ( $( "button" ).index( this ) ) {
case 0 :
value = $( "div" ).data( "blah" );
alert($( "div" ).data( "blah" ));
break;
case 1 :
$( "div" ).data( "blah", "hello" );
value = "Stored!";
alert($( "div" ).data( "blah" ));
break;
case 2 :
$( "div" ).data( "blah", 86 );
value = "Stored!";
alert($( "div" ).data( "blah" ));
break;
case 3 :
$( "div" ).removeData( "blah" );
value = "Removed!";
alert($( "div" ).data( "blah" ));
break;
}
$( "span" ).text( "" + value );
});
});
//]]>
</script>
</head>
<body>
<div>A div</div>
<button>Get "blah" from the div</button>
<button>Set "blah" to "hello"</button>
<button>Set "blah" to 86</button>
<button>Remove "blah" from the div</button>
<p>The "blah" value of this div is <span>?</span></p>
</body>
</html>
相关文章:
- 没有找到相关文章