2016-06-07 103 views
-2

爲什麼這不起作用:如何獲得小孩div的內容?

<div class="store" id="vframestore"> 
    <div>http://www.youtube.com/embed/_6DPBCOukLU?showinfo=0&modestbranding=1&fs=0&rel=0</div> 
    <div>http://www.youtube.com/embed/ZMr0q4Jz_uM?showinfo=0&modestbranding=1&fs=0&rel=0</div> 
</div> 

JS

var a = $("#vframestore").children[0].html; 
console.log(a); 

error: Uncaught TypeError: Cannot read property 'html' of undefined

+1

的可能的複製[jQuery的孩子得到專區內的值(http://stackoverflow.com/questions/2312876/jquery-get- value-within-child-div) – Megha

+1

'var a = $(「#vframestore」)。children()[0];' –

回答

0

你可以像下面。

var a = $("#vframestore").children().first().html(); 

或者

var a = $("#vframestore div").eq(0).html(); 
0

香草JS:

document.querySelector('#vframestore > DIV').innerHTML 
0

你可以使用一個CSS選擇器?

var a = $("#vframestore div:first-child").html(); 
0

var parent = document.getElementById("vframestore"); 
 
var child = parent.children[0].innerHTML; 
 
//alert(child); 
 
console.log(child);
<div class="store" id="vframestore"> 
 
    <div>http://www.youtube.com/embed/_6DPBCOukLU?showinfo=0&modestbranding=1&fs=0&rel=0</div> 
 
    <div>http://www.youtube.com/embed/ZMr0q4Jz_uM?showinfo=0&modestbranding=1&fs=0&rel=1</div> 
 
</div>

0

使用jQuery爲獲得必需的子股利和.html()用於獲取孩子的div內容的eq()

Jquery's .eq()

Jquery's .html()

$(document).ready(function() { 
 
    var childDiv1 = $('#vframestore div').eq(0).html(); 
 
    var childDiv2 = $('#vframestore div').eq(1).html(); 
 

 
    // alert child div contents 
 
    alert(childDiv1); 
 
    alert(childDiv2); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="store" id="vframestore"> 
 
    <div>http://www.youtube.com/embed/_6DPBCOukLU?showinfo=0&modestbranding=1&fs=0&rel=0</div> 
 
    <div>http://www.youtube.com/embed/ZMr0q4Jz_uM?showinfo=0&modestbranding=1&fs=0&rel=0</div> 
 
</div>