javascript - 在 url 中选择 div 时是否有 css/javascript 属性?

标签 javascript jquery css

例如,如果我有 URL example.com/#foo,那么样式将应用于 div foo。如果这在纯 css 中不存在,那么在 javascript/jquery 中执行此操作的有效且可靠的方法是什么?

最佳答案

您可以使用 window.location.hash 获取哈希值

假设 div 有一个 id = hash

JS:

var div = document.getElementById(window.location.hash);
if (div) {
  div.style.... = ...
}

jQuery

$('#' + window.location.hash).css({...})

关于javascript - 在 url 中选择 div 时是否有 css/javascript 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48969007/

相关文章:

HTML 在调整窗口大小时改变位置

Wordpress 插件 z-index 覆盖不起作用

javascript - 当条件满足时运行函数?

java - 在javascript下使用表达式语言?

javascript - Magento 的 CSS 缩小默认功能不起作用

javascript - 如何更改axios url?

javascript - 如何使 PDF 表格的数据表宽度为 100%

javascript - react /jquery : redirecting to another URL with built query string

javascript - jquery animate,设置间隔和非事件窗口问题

javascript - 悬停时更改 GridView