2015-07-20 152 views
0

我有一些页面需要改变它的“源”标签“src”属性,但现在我无法获得任何地方。 事情是,我可以使用getelemtbyId但这些“源”标签没有ID。 下面是一个例子:如何使用javascript更改src属性?

<section class="video-player"> 
<div id="91443f0a97f34c1294d05a8cad2d1bcc"> 
<video __idm_id__="297986"> 
<source src="https://example.org/1.mp4"> 
This browser cannot play .mp4, .ogg, or .webm files.Try using a different browser, such as Google Chrome. 
</video> 
</div> 
<h3 class="hidden">No playable video sources found.</h3> 
</section> 

我搜索,看到别的话题,我的问题是在3个方面的不同:
1-源标签没有标识。
2-我需要更改所有源标签的src属性,它们是多少,1,3或10.
3-我只能使用普通的javascript。

谢谢!
更新1: 那么,我试图在上下文中描述问题,所以它可能对别人有用,但我真的被困在这里!
问题是我正在尝试创建一个chrome的tampermonkey脚本,将src="https://example.com/ex.mp4"转换为src="http://example.com/ex.mp4",因为我的公司非常重视https流量,所以我们无法观看视频教程。
没有使用下面的答案我无法实现这一点。只需将src属性中的https更改为http即可。

+0

可以使用'document.getElementsByTagName( “源”)'来检索他们。 – Hacketo

+0

document.querySelectorAll('source') – JavaScript

回答

0

您可以使用

var source = document.getElementsByTagName('source'); 
//the source element will be array of all source elements in the html 
    var src = source[0].getAttribute("src"); //old Source of first source element 
    var newsrc = source[0].setAttribute("src",'new source value'); //set new src of first source element 
+0

我使用了这个方法,以及'waitForKeyElements(“source”,changeSrc);'因为内容是使用ajax服务的,这就是为什么我一开始就无法让它工作,只需要等到它完全加载。谢谢。 – Sam

0

您可以使用document.body.querySelectorAll这个

var sourcesNodeList = document.body.querySelectorAll('source'); 

// cast it to an array so you can use iterative methods on it 
Array.prototype.sourcesNodeList.forEach(function (source) { 
    // do something with source.src 
}); 
0

如果你只是想获得源代码,那么你可以使用选择器来video source让所有的人:

// Use more detailed selector to get the one on your example. 
 
var sources = document.querySelectorAll('video[__idm_id__="297986"] source'); 
 
var len = sources.length, i; 
 
for (i = 0; i < len; ++i) { 
 
    // Apply something to source. 
 
    console.log(sources[i].src); 
 
}
<section class="video-player"> 
 
<div id="91443f0a97f34c1294d05a8cad2d1bcc"> 
 
<video __idm_id__="297986"> 
 
<source src="https://example.org/1.mp4"> 
 
This browser cannot play .mp4, .ogg, or .webm files.Try using a different browser, such as Google Chrome. 
 
</video> 
 
</div> 
 
<h3 class="hidden">No playable video sources found.</h3> 
 
</section>

0

可以尝试用:

var source = document.getElementsByTagName("source") 

for (var i = 0; i < source.length; i++) { 
    source[i].src = 'LINK HERE'; 
}