gpt4 book ai didi

javascript - "Focus"动态链接事件

转载 作者:太空宇宙 更新时间:2023-11-04 16:31:23 25 4
gpt4 key购买 nike

我想在每个链接上应用“焦点”效果,这很简单但不像真正的 css“焦点”:

var backgroundColor, color, links = container.getElementsByTagName ("a");

for (i = 0; i < links.length; i ++) {
links[i].onblur = function () {
this.style.backgroundColor = backgroundColor;
this.style.color = color;
}

links[i].onfocus = function () {
backgroundColor = document.defaultView.getComputedStyle (this, null).getPropertyValue ("background-color");
color = document.defaultView.getComputedStyle (this, null).getPropertyValue ("color");

this.style.backgroundColor = "blue";
this.style.color = "white";
}
}

让我们动态添加更多链接,效果不会应用到它们,不像真正的 css “focus”:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en">
<head>
<meta http-equiv = "content-type" content = "text/html; charset=utf-8" />

<script type = "text/javascript">
window.onload = function () {
var container = document.getElementsByTagName ("div")[0];

var i = 0;

container.innerHTML = "Focus on the links, you have 10 seconds, <b>" + i + "<b>";

for (i = 1; i <= 5; i ++) {
var a = document.createElement ("a");
a.innerHTML = i;
a.setAttribute ("href", "#");

container.appendChild (a);
}

i = 0;

var interval = window.setInterval (function () {
i ++;

if (i < 10) {
container.childNodes[1].innerHTML = i;
}
else {
window.clearInterval (interval);

container.innerHTML = "No more effects, different links";

for (i = 1; i <= 10; i ++) {
var a = document.createElement ("a");
a.innerHTML = i;
a.setAttribute ("href", "#");

container.appendChild (a);
}
}
}, 1000);

var backgroundColor, color, links = container.getElementsByTagName ("a");

for (i = 0; i < links.length; i ++) {
links[i].onblur = function () {
this.style.backgroundColor = backgroundColor;
this.style.color = color;
}

links[i].onfocus = function () {
backgroundColor = document.defaultView.getComputedStyle (this, null).getPropertyValue ("background-color");
color = document.defaultView.getComputedStyle (this, null).getPropertyValue ("color");

this.style.backgroundColor = "blue";
this.style.color = "white";
}
}
}
</script>

<style type = "text/css">
a {
border: 1px solid black;
display: block;
margin: 5px;
padding: 5px;
text-decoration: none;
}
*/
a:focus {
background-color: blue;
color: white;
}*/
</style>

<title>Olá</title>
</head>

<body><div></div></body>
</html>

我能做些什么来扭转它?

谢谢

最佳答案

您可以处理 <body> 上的事件标签;它们会冒泡并到达那里。

jQuery 会用它的 live method 为你做这件事.

关于javascript - "Focus"动态链接事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com