JavaScript

来自百合仙子's Wiki
(重定向自Javascript
跳转到导航 跳转到搜索
本页主题是JavaScript 在网页中的使用,关于JavaScript 语言本身,见 JavaScript 语言

位置相关

获取窗口的高度/宽度,对火狐/Chrome有效

window.innerHeight
window.innerWidth

参见A tale of two viewports — part one

元素的位置

offsetTopoffsetLeft 是相对于 offsetParent (如果支持的话)的位置。[1]

元素的大小:

el.offsetWidth
el.offsetHeight

滚动条

当前高度(两者可能相差小于 1)

window.scrollY
document.documentElement.scrollTop

最大高度

document.documentElement.scrollHeight

将页面滚动条移动到结尾:

window.scrollTo(0, document.documentElement.scrollHeight - document.documentElement.clientHeight)

DOM

document 对象

关于 document.referrer :: oldj.net,不同浏览器的支持情况。

select 和 option

设置一个下拉菜单的值的方法:

selectEl.value = "corresponding value";
optionEl.selected = true;

火狐(20)中,使用 el.setAttribute('selected', 'selected') 也可以设置下拉菜单的选中项。在 Google Chrome(28)中,此方法无效,并且会导致其 selected 属性变为 true

检测 document.title 的更新

[2]

const observer = new MutationObserver(function(_mutationList, _observer) {
  // do sth
})

observer.observe(
  document.querySelector('head > title'),
  { subtree: true, characterData: true, childList: true })

UI

图形

跨浏览器相关

测试

其它

参见

外部链接

文档

工具

参考资料