如何获取点击或标签以选择北,西或中心面板? 这是标签代码有效,但不知道如何将其与点击面板代码集成
JAVASCRIPT:
$(document).ready(function(){
var divs = ["north", "west", "center"];
var startIndex = 0;
$(document).keydown(function(e) {
if (e.which == 9) {
$("div").css("border", "");
$("#" + divs[startIndex]).css("border", "4px solid gray");
if(divs[startIndex] == "north"){
alert("north");
}else if(divs[startIndex] == "west"){
alert("west");
}else{
alert("center");
}
startIndex++;
if(startIndex === divs.length) {
startIndex = 0;
}
}
return false;
});
});
HTML:
<div id="north"><textarea></textarea></div>
<div id="west"><textarea></textarea></div>
<div id="center"><textarea></textarea></div>
答案 0 :(得分:1)
假设您在单击时需要执行相同的功能,则需要使用以下内容:
$(document).ready(function() {
var divs = ["north", "west", "center"];
var startIndex = 0;
var fn = function (e) {
$("div").css("border", "");
$("#" + divs[startIndex]).css("border", "4px solid gray");
if(divs[startIndex] == "north") {
console.log("north");
} else if(divs[startIndex] == "west") {
console.log("west");
}else {
console.log("center");
}
startIndex++;
if(startIndex === divs.length) {
startIndex = 0;
}
};
$(document).keydown(function(e) {
if (e.which == 9) {
fn();
}
return false;
}).click(function() {
//any logic like the 'tab' check above
fn();
});
});
有一个常用函数用于执行背景突出显示,并且可以从keydown
或click
句柄调用。
在这里演示 - http://jsfiddle.net/slace/D39P3/