我在獲取兩個標簽的id(aId,bId)之後,想將這兩個標簽拼在一起,然後打印出來
for(var i = 0;i<aLi.length;i++){
aLi[i].onclick = function(){
var aTxt = this.innerHTML;
window.aId = this.id;
document.getElementById('ss').innerHTML = aTxt;
}
}
for(var j = 0;j<bLi.length;j++){
bLi[j].onclick = function(){
var bTxt = this.innerHTML;
window.bId = this.id;
document.getElementById('cc').innerHTML = bTxt;
}
}
var addId = aId + bId;
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
alert(addId);
}
這麼寫可以麼?
<p id="t1">第一題您選擇的是<strong><u id="ss"></u></strong></p>
<ul id="choose1">
<li id="a1">哈哈</li>
<li id="a2">呵呵</li>
<li id="a3">嘿嘿</li>
<li id="a4">吼吼</li>
</ul>
<p id="t2">第二題您選擇的是<strong><u id="cc"></u></strong></p>
<ul id="choose2">
<li id="b1">java</li>
<li id="b2">ios</li>
<li id="b3">Android</li>
<li id="b4">php</li>
<li id="b5">jquery</li>
</ul>
<input type="button" id="btn" value="答案"/>
<script type="text/javascript">
var addId, aId, bId;
window.onload = function () {
var aLi = document.getElementById('choose1').getElementsByTagName('li');
var bLi = document.getElementById('choose2').getElementsByTagName('li');
for (var i = 0; i < aLi.length; i++) {
aLi[i].onclick = function () {
var aTxt = this.innerHTML;
aId = this.id;
document.getElementById('ss').innerHTML = aTxt;
}
}
for (var j = 0; j < bLi.length; j++) {
bLi[j].onclick = function () {
var bTxt = this.innerHTML;
bId = this.id;
document.getElementById('cc').innerHTML = bTxt;
}
}
}
var oBtn = document.getElementById('btn');
oBtn.onclick = function () {
addId = (aId || '') + (bId || '');
alert(addId);
}
</script>