Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
Regex 从document.write()中提取URL_Regex_Url_Find_Extract_Document.write - Fatal编程技术网

Regex 从document.write()中提取URL

Regex 从document.write()中提取URL,regex,url,find,extract,document.write,Regex,Url,Find,Extract,Document.write,我有一个简单的html页面,它提供了一个javascript文件。javascript文件的唯一用途是编写以下内容 document.write('<a href="http://www.linktosite.com"><img src="http://www.location.com/image.png"></a>'); document.write(“”); 一旦信息被写入,我需要一些javascript来提取url和图像源,并单独返回url和图像位置

我有一个简单的html页面,它提供了一个javascript文件。javascript文件的唯一用途是编写以下内容

document.write('<a href="http://www.linktosite.com"><img src="http://www.location.com/image.png"></a>');
document.write(“”);
一旦信息被写入,我需要一些javascript来提取url和图像源,并单独返回url和图像位置

感谢您的帮助。提前谢谢你

查看下面的内容,它向您展示了如何使用JQuery或纯Javascript进行操作

更新:

如果您有能力修改HTML,那么为什么不在插入图像的位置之后添加一个DOM元素,以便可以直接挂接到该元素上呢?然后可以使用以下JQuery:

var linkDest = $('#Anchor').prev().attr('href');
var imgSrc = $('#Anchor').prev().children().attr('src');

您可以在

中看到,您可以修改该JS文件吗?如果是,请向锚添加一个
id
属性,然后使用
document.getElementById()
检索它。不幸的是,我无法修改JS文件。我觉得你的答案是正确的,但他们的方法要求我在标记中有一个id。我没有权限修改该信息。您有能力向html添加任何内容吗?是的,我可以向html添加任何内容。请参阅更新的答案(以及附带的JSFIDLE示例),并告知是否有可能。如果没有,请解释问题是什么,以便我们能够构建一个可行的解决方案。这在JSFIDLE中非常有效!当我将代码插入我的站点时,我会得到不同的结果。这是我目前的代码。我还包括jQuery1.7.2。警报($('#Anchor').prev().attr('href'));警报($('#Anchor').prev().children().attr('src'));