使用JS

时间:2015-09-17 06:09:43

标签: javascript jquery html json

我从服务中返回了以下json:

{
"html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/FQpUOimNvXA?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>"
}

如何使用javascript获取src属性值,即https://www.youtube.com/embed/FQpUOimNvXA?feature=oembed

2 个答案:

答案 0 :(得分:5)

iframe创建一个jQuery对象并获取src属性:

var jsonString = {"html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/FQpUOimNvXA?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>"}
var src = $(jsonString.html).attr("src");
$("#source").html(src);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="source"></div>

答案 1 :(得分:1)

FIDDLE

var data = {
"html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/FQpUOimNvXA?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>"
}

console.log(data.html);

var str = data.html;
var word = str.split(" ");
var word1 = word[3].split('"');
console.log(word1[1]);

我做了几次分手,最后我https://www.youtube.com/embed/FQpUOimNvXA?feature=oembed