请问如何把javascript改为jQuery?
varnumber=Math.floor(Math.random()*9);vartimes=0;functiononImgClick(){this.setAttribu...
var number=Math.floor(Math.random()*9);
var times=0;
function onImgClick(){this.setAttribute("src","open.jpg");
if( this.getAttribute("number")==number)
{alert("It cost you "+times+" times to get it right!" );
this.setAttribute("src","kittens.jpg");}
else{times++;}
}
for(var i=0; i<9; i++){var img=document.createElement("img");
img.setAttribute("src", "closed.jpg");
img.setAttribute("number",i);
document.body.appendChild(img);
img.onclick=onImgClick;
}
怎么改为jQuery?请问下面的代码如何修改?
var number=Math.floor(Math.random()*9);
var times=0;
for(var i=0; i<9; i++){
var img=$("<img>").prop("src","closed.jpg");
$("body").append(img);
$("<img>").prop(number, i);
}
$(function(){
$("<img>").click(function(){
$(this).prop("src","open.jpg");
if($(this).attr("number")==number){
alert("It cost you "+times+" times to get it right!");
$(this).prop("src","kittens.jpg");
}
else{times++;
}
}) 展开
var times=0;
function onImgClick(){this.setAttribute("src","open.jpg");
if( this.getAttribute("number")==number)
{alert("It cost you "+times+" times to get it right!" );
this.setAttribute("src","kittens.jpg");}
else{times++;}
}
for(var i=0; i<9; i++){var img=document.createElement("img");
img.setAttribute("src", "closed.jpg");
img.setAttribute("number",i);
document.body.appendChild(img);
img.onclick=onImgClick;
}
怎么改为jQuery?请问下面的代码如何修改?
var number=Math.floor(Math.random()*9);
var times=0;
for(var i=0; i<9; i++){
var img=$("<img>").prop("src","closed.jpg");
$("body").append(img);
$("<img>").prop(number, i);
}
$(function(){
$("<img>").click(function(){
$(this).prop("src","open.jpg");
if($(this).attr("number")==number){
alert("It cost you "+times+" times to get it right!");
$(this).prop("src","kittens.jpg");
}
else{times++;
}
}) 展开
展开全部
var number = Math.floor ( Math.random() * 9 );
var times = 0;
function onImgClick ()
{
var that = $(this);
that.prop ("src", "open.jpg");
if ( that.prop ("number") == number)
{
alert ("It cost you " + times + " times to get it right!" );
that.prop ("src", "kittens.jpg");
}
else
{
times++;
}
}
for ( var i = 0; i < 9; i++ )
{
var img = $ ("<img />");
img.prop ("src", "closed.jpg");
img.prop ("number", i);
img.click(function() {
onImgClick();
});
$(document.body).append (img);
}
---------------------------------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询