我基本上li
在单击时突出显示每个,并且希望li
在单击其他地方或跳出(通过影响background-color
属性)时“取消突出显示”单击的内容。
这种行为本质上是在模仿<select>
它的突出显示行为......虽然我没有使用select
,因为我想将 HTML 嵌套在列出的项目中—— 你不能用<option>
.
我正在尝试使用onblur
,它不起作用......
这是HTML:
<ul id = "list">
<li>asdf</li>
<li>qwerty</li>
<ul>
...这是CSS:
#list {
list-style-type: none;
}
...这里是 jQuery/Javascript:
function getEventTarget(e) {
e = e || window.event;
return e.target || e.srcElement;
}
function highlightSelection(selection) {
alert("It worked!");
$(selection).css("background-color","#FFFF00");
}
// this function is not being triggered:
function removeHighlight(selection) {
$(selection).css("background-color","none");
}
var ul = document.getElementById("list");
ul.onclick = function(event) {
var target = getEventTarget(event);
highlightSelection(target);
};
// this event is not working:
ul.onblur = function(event) {
var target = getEventTarget(event);
removeHighlight(target);
};