在目标中单击的对象是否有唯一 ID?
Is there a unique ID for objects clicked within target?
使用以下代码,正如您所见,创建的每个椭圆形名称始终保持不变。
所以无论我点击哪个椭圆,我都会得到相同的名字。目标属性中是否隐藏了某种唯一 ID?
<!DOCTYPE html>
<html lang="en">
<input type="button" onClick="doit()" value="Do it">
<script>
function doit() {
drawOval(160,80,50);
myOval = document.getElementById("newdiv");
}
function drawOval(width, height, radius) {
if (document.createElement) {
newdiv=document.createElement("div");
newdiv.style.width = width+"px";
newdiv.style.height = height+"px";
newdiv.style.backgroundColor = 'red';
newdiv.style.visibility = 'visible';
newdiv.style.borderRadius = radius+"%"
newdiv.id = 'newdiv';// + i;
document.body.appendChild(newdiv);
}
}
document.onclick = function(f){
alert(f.target.id)
}
</script>
</html>
试试这个
<!DOCTYPE html>
<html lang="en">
<input type="button" onClick="doit()" value="Do it">
<script>
var id = 1;
function doit() {
drawOval(160,80,50);
myOval = document.getElementById("newdiv");
}
function drawOval(width, height, radius) {
if (document.createElement) {
newdiv=document.createElement("div");
newdiv.style.width = width+"px";
newdiv.style.height = height+"px";
newdiv.style.backgroundColor = 'red';
newdiv.style.visibility = 'visible';
newdiv.style.borderRadius = radius+"%"
newdiv.id = 'newdiv' + id;// + i;
id++;
document.body.appendChild(newdiv);
}
}
document.onclick = function(f){
alert(f.target.id)
}
</script>
</html>
使用以下代码,正如您所见,创建的每个椭圆形名称始终保持不变。 所以无论我点击哪个椭圆,我都会得到相同的名字。目标属性中是否隐藏了某种唯一 ID?
<!DOCTYPE html>
<html lang="en">
<input type="button" onClick="doit()" value="Do it">
<script>
function doit() {
drawOval(160,80,50);
myOval = document.getElementById("newdiv");
}
function drawOval(width, height, radius) {
if (document.createElement) {
newdiv=document.createElement("div");
newdiv.style.width = width+"px";
newdiv.style.height = height+"px";
newdiv.style.backgroundColor = 'red';
newdiv.style.visibility = 'visible';
newdiv.style.borderRadius = radius+"%"
newdiv.id = 'newdiv';// + i;
document.body.appendChild(newdiv);
}
}
document.onclick = function(f){
alert(f.target.id)
}
</script>
</html>
试试这个
<!DOCTYPE html>
<html lang="en">
<input type="button" onClick="doit()" value="Do it">
<script>
var id = 1;
function doit() {
drawOval(160,80,50);
myOval = document.getElementById("newdiv");
}
function drawOval(width, height, radius) {
if (document.createElement) {
newdiv=document.createElement("div");
newdiv.style.width = width+"px";
newdiv.style.height = height+"px";
newdiv.style.backgroundColor = 'red';
newdiv.style.visibility = 'visible';
newdiv.style.borderRadius = radius+"%"
newdiv.id = 'newdiv' + id;// + i;
id++;
document.body.appendChild(newdiv);
}
}
document.onclick = function(f){
alert(f.target.id)
}
</script>
</html>