gpt4 book ai didi

jquery - 使用 get 关注目标 div 元素

转载 作者:行者123 更新时间:2023-11-28 05:35:12 25 4
gpt4 key购买 nike

我有一个页面,内容使用唯一 ID 列出,我希望每当用户单击链接查看任何这些元素时,它会向下或向上滚动以在地址栏中找到具有该 ID 的元素 foo=or2 ,同时它会将 css 焦点样式应用于该元素。我试过这样做,但对我没有用,有人可以帮忙吗?

Jquery代码

$(document).ready(function (){
// I don't know how to get the id from url
$('div').focus();
// I also don't know what else to do to make it work
});

Here is my example url to focus on div element

http://www.example.com?see=orang$foo=or2

HTML 示例

    div:focus {
background: red;
}
<div id="or1">Orange</div>
<div id="or2">Mango</div>
<div id="or3">Banana</div>
<div id="or4">Pear</div>

最佳答案

这似乎是一个随意添加的东西,但如果您将 tabindex="-1"添加为 div 的属性,它们应该是可聚焦的,该函数只有一组有限的元素可以在没有它的情况下定位。

这是来自 .focus jQuery 文档:

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (input, select, etc.) and links (a href). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

关于jquery - 使用 get 关注目标 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38273311/

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