我有一个字符串
str = '<iframe width="100%" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=vi&geocode=&q=Vimcom+91+b%C3%A0+tri%E1%BB%87u&aq=&sll=15.125395,108.795111&sspn=0.034096,0.038581&ie=UTF8&hq=Vimcom+91+b%C3%A0+tri%E1%BB%87u&hnear=&radius=15000&t=m&ll=21.011605,105.849323&spn=0.048074,0.051498&z=13&output=embed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&source=embed&hl=vi&geocode=&q=Vimcom+91+b%C3%A0+tri%E1%BB%87u&aq=&sll=15.125395,108.795111&sspn=0.034096,0.038581&ie=UTF8&hq=Vimcom+91+b%C3%A0+tri%E1%BB%87u&hnear=&radius=15000&t=m&ll=21.011605,105.849323&spn=0.048074,0.051498&z=13" style="color:#0000FF;text-align:left">Xem Bản đồ cỡ lớn hơn</a></small>'
如何使用str字符串中的javascript(no-jquery)获取attr src? 谢谢!
答案 0 :(得分:0)
试试这个:
str.match(/.*src="([^"]+).*/)[1]
答案 1 :(得分:0)
你可以这样做:
var str = '<iframe width="100%" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=vi&geocode=&q=Vimcom+91+b%C3%A0+tri%E1%BB%87u&aq=&sll=15.125395,108.795111&sspn=0.034096,0.038581&ie=UTF8&hq=Vimcom+91+b%C3%A0+tri%E1%BB%87u&hnear=&radius=15000&t=m&ll=21.011605,105.849323&spn=0.048074,0.051498&z=13&output=embed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&source=embed&hl=vi&geocode=&q=Vimcom+91+b%C3%A0+tri%E1%BB%87u&aq=&sll=15.125395,108.795111&sspn=0.034096,0.038581&ie=UTF8&hq=Vimcom+91+b%C3%A0+tri%E1%BB%87u&hnear=&radius=15000&t=m&ll=21.011605,105.849323&spn=0.048074,0.051498&z=13" style="color:#0000FF;text-align:left">Xem Bản đồ cỡ lớn hơn</a></small>';
var div = document.createElement('div');
div.innerHTML = str;
alert(div.childNodes[0].getAttribute('src'));
http://jsfiddle.net/Dogbert/hxjpB/
确保字符串以<iframe>
开头,或者第一个childNode是textNode,这不起作用。 (如果你愿意的话,还有更强大的方法可以在这些情况下使用。)
答案 2 :(得分:0)
您可以在代码中使用正则表达式找到有趣的内容:http://www.w3schools.com/jsref/jsref_obj_regexp.asp和http://www.w3schools.com/js/js_obj_regexp.asp
这个正则表达式应该有效:
/src="[^\ ]*"/i