简体   繁体   English

如何从字符串获取href src值

[英]How to get the href src value from a string

在此处输入图片说明 A string contains entire value of a html.i need to get the value of href tag ie src value.please help me to solve. 一个字符串包含整个html.i的值,我需要获取href标记的值,即src value.please帮助我解决。

         var alltext="a href="images/BGL30NA-10.JPG" target="""
         var str=allText;

if you using jQuery: 如果您使用jQuery:

try this: 尝试这个:

var href = $('a').attr('href')

but better if you have <a></a> with something id and then 但更好的是,如果您有<a></a>并带有id ,然后

var href = $('#youId').attr('href')
'a href="images/BGL30NA-10.JPG" target=""'.match(/href="(.*?)"/)[1]

试试这个正则表达式。

hope this may help you. 希望对您有帮助。

try this, it will gives the value of href in javascript , below work only if you assign id attribute in your anchor tag <a href="images\\images1.jpg" id="image1"> 试试这个,它会给出HREF中值javascript ,下面的工作只有当你分配id在你的锚标记属性<a href="images\\images1.jpg" id="image1">

for only value of href document.getElementById("image1").getAttribute("href"); 仅用于href document.getElementById("image1").getAttribute("href"); here is Jsfiddle 这是Jsfiddle

if you want to get full path then use this: 如果您想获取完整路径,请使用以下命令:

for full path of href document.getElementById("aaa").href; 用于href document.getElementById("aaa").href; 完整路径 document.getElementById("aaa").href; here is Jsfiddle 这是Jsfiddle

if you have any doubts, ask it in below comment. 如果您有任何疑问,请在下面的评论中提问。 @sriram @sriram

Parse the string before and use appropriate DOM functions: 之前解析字符串并使用适当的DOM函数:

str = "hello, <b>my name is</b> jQuery.";
html = $.parseHTML( str );
# use $('a').attr('href') or any other selector here

The mandatory link to TONY THE PONY . TONY THE PONY的强制性链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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