3.9 KiB
3.9 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7790367417b2b2512ab0 | Use tabindex to Add Keyboard Focus to an Element | 0 | 使用tabindex将键盘焦点添加到元素 |
Description
tabindex
属性有三个与元素键盘焦点相关的不同功能。当它在标签上时,它表示可以关注元素。值(一个正数,负数或零的整数)决定了行为。当用户在页面中进行选项卡时,某些元素(如链接和表单控件)会自动接收键盘焦点。它与HTML源标记中的元素的顺序相同。通过在其上放置tabindex="0"
属性,可以将相同的功能赋予其他元素,例如div
, span
和p
。这是一个例子: <div tabindex="0">I need keyboard focus!</div>
注意 负
tabindex
值(通常为-1)表示元素是可聚焦的,但键盘无法访问。此方法通常用于以编程方式将焦点集中到内容上(例如,当激活用于弹出窗口的div
时),并且超出了这些挑战的范围。 Instructions
tabindex
属性添加到p
标记并将其值设置为“0”。奖金 - 使用tabindex
还可以使CSS伪类:focus
在p
标签上工作。 Tests
tests:
- text: 您的代码应该将<code>tabindex</code>属性添加到包含表单指令的<code>p</code>标记。
testString: 'assert($("p").attr("tabindex"), "Your code should add a <code>tabindex</code> attribute to the <code>p</code> tag that holds the form instructions.");'
- text: 您的代码应将<code>p</code>标记上的<code>tabindex</code>属性设置为值0。
testString: 'assert($("p").attr("tabindex") == "0", "Your code should set the <code>tabindex</code> attribute on the <code>p</code> tag to a value of 0.");'
Challenge Seed
<head>
<style>
p:focus {
background-color: yellow;
}
</style>
</head>
<body>
<header>
<h1>Ninja Survey</h1>
</header>
<section>
<form>
<p>Instructions: Fill in ALL your information then click <b>Submit</b></p>
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br>
<fieldset>
<legend>What level ninja are you?</legend>
<input id="newbie" type="radio" name="levels" value="newbie">
<label for="newbie">Newbie Kitten</label><br>
<input id="intermediate" type="radio" name="levels" value="intermediate">
<label for="intermediate">Developing Student</label><br>
<input id="master" type="radio" name="levels" value="master">
<label for="master">9th Life Master</label>
</fieldset>
<br>
<fieldset>
<legend>Select your favorite weapons:</legend>
<input id="stars" type="checkbox" name="weapons" value="stars">
<label for="stars">Throwing Stars</label><br>
<input id="nunchucks" type="checkbox" name="weapons" value="nunchucks">
<label for="nunchucks">Nunchucks</label><br>
<input id="sai" type="checkbox" name="weapons" value="sai">
<label for="sai">Sai Set</label><br>
<input id="sword" type="checkbox" name="weapons" value="sword">
<label for="sword">Sword</label>
</fieldset>
<br>
<input type="submit" name="submit" value="Submit">
</form><br>
</section>
<footer>© 2018 Camper Cat</footer>
</body>
Solution
// solution required