Javascript /改变文本背景颜色

时间:2014-11-17 05:17:38

标签: javascript

这段代码在5行中放置5个随机数,当用户点击每个数字时,光标形状变为指针,文本背景颜色随机变化。 我的意思是这样做,当鼠标进入文本本身时,它变成一个指针,只有文本的颜色不会改变整行。但我不知道该怎么做。任何提示?

var div = document.getElementById("div1"); // There is a html file with a div tag which its ID is div1
var bgColors = ["yellow","red","green","blue","orange"];

for(counter = 0 ; counter<5; counter++){
    p = document.createElement("p");
    node = document.createTextNode(generateRandomNumer());
    p.appendChild(node);
    div.appendChild(p);
}

function bgColorChange(element){    
    element.style.backgroundColor = bgColors[Math.floor((Math.random()*5))];   
}

pTags = document.getElementsByTagName("p");
for (counter = 0; counter<pTags.length ; counter++ ){
    console.log(pTags[counter]);
    pTags[counter].style.cursor = "pointer"
    pTags[counter].addEventListener("click",function(){      
        bgColorChange(this)});
};

0 个答案:

没有答案