javascript 使用javascript而不是jquery通过src属性获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19252339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:53:44  来源:igfitidea点击:

Get element by src attribute with javascript not jquery

javascript

提问by lbennet

How to get the element by its src attribute. I'm using:

如何通过 src 属性获取元素。我正在使用:

 <script type="text/javascript" src="mysrc.js?id=1000">

I would need to get that element on a variable. All I know is getAttribute("src")returns the src if I knew anything else besides the actual src whcih is all I have. I can't use jQuery, plain JS.

我需要在变量上获取该元素。我所知道的就是getAttribute("src")返回 src,如果我知道除了实际 src 之外的任何其他信息,我所拥有的。我不能使用 jQuery,纯 JS。

Thanks

谢谢

回答by prakashapkota

You can do like this

你可以这样做

<script id="mysrc" type="text/javascript" src="mysrc.js?id=1000">

var src = document.getElementById("mysrc").getAttribute("src")
console.log(src.split('id=')[1]); //1000