简体   繁体   中英

How can I do `$('meta[name=“csrf-token”]').attr('content')` in vanilla Javascript?

I don't have jQuery and I want to know how can I do the same thing with vanilla javascript. Any help?

$('meta[name="csrf-token"]').attr('content')
document.querySelector('meta[name="csrf-token"]').getAttribute('content');

你就是这样做的。

meta elements actually have a content property , so you can simply write:

document.querySelector('meta[name=csrf-token]').content

Demo:

 console.log( document.querySelector('meta[name=csrf-token]').content ) 
 <meta name="csrf-token" content="example-content"/> 

您也可以使用getElementsByName

document.getElementsByName('csrf-token')[0].getAttribute('content')

使用querySelector()方法获取元素并使用getAttribute()方法从元素中获取属性值。

document.querySelector('meta[name="csrf-token"]').getAttribute('content') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM