const parts = document.querySelector('.part1') // console.log(parts); const btn = document.querySelector('.btn-inner-btn') const c_tx_highlight = document.querySelector('.comment__tips') const tx = document.querySelector('#tx') tx.addEventListener('focus', function() { c_tx_highlight.style.opacity = 0 })这些代码什么意思
时间: 2023-05-21 21:04:32 浏览: 84
这段代码使用了querySelector方法来选择页面中的元素。通过使用'.part1'类名,选择了class为part1的元素。同样,使用'.btn-inner-btn'类名和'.comment__tips'类名来选择对应的元素。在最后使用了querySelector方法来选择第一个匹配' '空格的元素。这些元素将被存储在变量parts、btn、c_tx_highlight和tx中。
相关问题
btn: document.querySelectorAll('.box button')和const btn=document.querySelector('.box buttin')一样吗
btn: document.querySelectorAll('.box button') 和 const btn = document.querySelector('.box button') 是不同的。
1. btn: document.querySelectorAll('.box button') 是一个使用 CSS 选择器来获取所有 class 为 'box' 的元素下的所有 button 元素的集合。返回的是一个 NodeList 对象,可以通过索引来访问其中的元素。
2. const btn = document.querySelector('.box button') 是使用 CSS 选择器来获取 class 为 'box' 的元素下的第一个 button 元素。返回的是一个单个元素,可以直接访问该元素的属性和方法。
所以,它们的作用是不同的,前者返回多个元素的集合,后者返回单个元素。
tx.addEventListener('blur', function() { c_tx_highlight.style.opacity = 1 }) const info = document.querySelector('.info') // console.log(info); btn.addEventListener('click', function(e) { e.preventDefault const val = tx.value if (tx.value === '') { alert('请输入内容') } else { info.innerHTML = val tx.value = '' parts.style.display = 'block' } })这串代码什么意思
这是一段 JavaScript 代码,它的作用是:当输入框失去焦点时,为其添加一个高亮效果;当按钮被点击时,获取输入框的值,如果它不为空,则将其赋值给 info 元素,并显示 parts 元素。如果输入框为空,则弹出一个提示框,要求用户输入内容。