下面JS代码怎么转换成Jquery代码
<divclass="tab"><divclass="c"id="c0">PHP</div><divclass="c"id="c1">JAVA</div><divclas...
<div class="tab">
<div class="c" id="c0">PHP</div>
<div class="c" id="c1">JAVA</div>
<div class="c" id="c2">C</div>
<div class="c" id="c3">C++</div>
<div class="c" id="c4">JS</div>
<div class="c" id="c5">Ruby</div>
<input id="i" type="text" style="width:720px; height:25px; border:1px solid #ccc"/>
</div>
<script>
var x=["PHP","JAVA","C","C++","JS","Ruby"];
var y=[];
function $(id){
return document.getElementById(id)
};
for(var i=0,m=x.length;i<m;i++){
$("c"+i).onclick=(function(i){
return function(){
var s=y.join(",").indexOf(x[i]);
if(s>=0){
for(var r in y){
if(y[r]==x[i]){y.splice(r,1)}
}
}
else{
y.push(x[i])
}
$("i").value=y.join(" ");
}
})
(i)
}
</script> 展开
<div class="c" id="c0">PHP</div>
<div class="c" id="c1">JAVA</div>
<div class="c" id="c2">C</div>
<div class="c" id="c3">C++</div>
<div class="c" id="c4">JS</div>
<div class="c" id="c5">Ruby</div>
<input id="i" type="text" style="width:720px; height:25px; border:1px solid #ccc"/>
</div>
<script>
var x=["PHP","JAVA","C","C++","JS","Ruby"];
var y=[];
function $(id){
return document.getElementById(id)
};
for(var i=0,m=x.length;i<m;i++){
$("c"+i).onclick=(function(i){
return function(){
var s=y.join(",").indexOf(x[i]);
if(s>=0){
for(var r in y){
if(y[r]==x[i]){y.splice(r,1)}
}
}
else{
y.push(x[i])
}
$("i").value=y.join(" ");
}
})
(i)
}
</script> 展开
1个回答
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<script src="jquery.js"></script>
<script>
jQuery(function(){
var x=['PHP','JAVA','C','C++','JS','Ruby'];
var y=[];
jQuery.each(x,function(idx,lang){
jQuery('#c'+ idx).click(function(){
var s = y.join(',').indexOf(lang);
s >= 0 ? jQuery.each(y,function(idx){
this == lang && y.splice(idx,1);
}) : y.push(lang);
jQuery('#i').val(y.join(' '));
});
});
});
</script>
</head>
<body>
<div class="tab">
<div class="c" id="c0">PHP</div>
<div class="c" id="c1">JAVA</div>
<div class="c" id="c2">C</div>
<div class="c" id="c3">C++</div>
<div class="c" id="c4">JS</div>
<div class="c" id="c5">Ruby</div>
<input id="i" type="text" style="width:720px; height:25px; border:1px solid #ccc"/>
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询