getAttribute

154 阅读1分钟

.getAttribute 只在函数中才能使用。setAttribute也是

const $ = s => document.querySelector(s)
$('button').onclick = function() {
   let href = $('a').getAttribute('href')
    console.log(href)

在后台用console.log打印

<a href="https://baidu.com">百度</a>

<script>
  let $a = document.querySelector('a')
  console.log( $a.getAttribute('href') )
</script>  

将会报错