Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript获取元素的链接?_Javascript_Html_Jquery_Dom_Href - Fatal编程技术网

如何使用javascript获取元素的链接?

如何使用javascript获取元素的链接?,javascript,html,jquery,dom,href,Javascript,Html,Jquery,Dom,Href,我想从动漫网站获取一张图片的url,这是图片url所在的位置 <a href="/anime/strike-witches-3- 775962/episode-03-692659" class="ka-url-wrapper video-item- poster rounded" cryptkey=" 8cefe99856a1e0da611 782dd2909018757142 568486877b1fd24ad7df4

我想从动漫网站获取一张图片的url,这是图片url所在的位置

<a href="/anime/strike-witches-3- 
 775962/episode-03-692659" 
 class="ka-url-wrapper video-item- 
 poster rounded"  cryptkey="
 8cefe99856a1e0da611
 782dd2909018757142
 568486877b1fd24ad7df4fa9cc1" 
 cryptiv="77a23d
 8ce0c90dc66559f3e610a2006a" 
 style="background:  
 url(&quot;https://
 www1.kickassanime.rs/
 uploads/026260.jpg&quot;) 
center center / cover no-repeat;">
<div class="video-item-badge 
rounded">SUB</div><div 
class="video-item-badge 
rounded">EP 03</div></a>
但系统的响应是

  /anime/strike-witches-3- 
  775962/episode-03-692659

请帮助我获取所需的url(粗体字母)

您可以使用DOM stye api获取背景图像url

element.style
属性让您知道在该元素中定义为内联的CSS属性(以编程方式,或在元素的style属性中定义)

一旦有了元素的样式,就可以从“style.background”属性获取背景图像url,然后可以使用正则表达式提取图像url

示例:

const elem=document.queryselectoral(“[style*=background]”[0];
const bg=elem.style.background.replace(/url\(“(.*?”\)./,“$1”)
控制台日志(bg)

您正在读取的href属性不包含图像url

您可以提取整个syle,然后使用一些正则表达式来提取
url(…)

var style=document.querySelector(“[style*=background]”)。style.background;
var regex=/url\((*?)\)*/
var url=regex.exec(style)[1]
console.log(url)

没有粗体字母-我想你想要背景url?
  /anime/strike-witches-3- 
  775962/episode-03-692659
document.querySelector(...).style.background.split('"')[1]