2009-09-16 8 views
5

मेरे पास निम्न HTMLजावास्क्रिप्ट स्क्रिप्ट ब्लॉक वाले div की सामग्री कैसे प्राप्त करें?

<div id="example"> 
    ...some text... 
    <script type="text/javascript"> 
    ... some javascript... 
    </script> 
</div> 

कैसे #example की सामग्री, लेकिन यह भी जावास्क्रिप्ट के साथ पाने के लिए?

$("#example").html(), 
$("#example").text(),  
$("#example").val()  

सभी काम नहीं करते हैं।

+4

अपने जावास्क्रिप्ट का नहीं है वहाँ – Natrium

+1

$ .html() काम किया जाना चाहिए था। मुझे आश्चर्य है क्योंकि। – mauris

उत्तर

5

Working Demo

आप

html() उपयोग कर सकते हैं: सबसे पहले मिलने वाला तत्व के HTML सामग्री (innerHTML) प्राप्त करें।

var contents = $("#example").html(); 
+1

जब मैं $ ("# उदाहरण") का उपयोग करता हूं। Html(); मुझे केवल div की सामग्री मिलती है, लेकिन उस जावास्क्रिप्ट के बिना, यह सभी जावास्क्रिप्ट –

11

html() विधि आपके लिए काम करनी चाहिए। क्या आप सुनिश्चित हैं कि डोम पूरा होने के बाद आप कोड चला रहे हैं?

$(document).ready(function(){ 
    alert($("#example").html()); 
}); 
+0

.html() को अनदेखा करता है, लौटाए गए HTML से सभी

1
var txt = document.getElementById("example").innerHTML; 

यह आपको div की innerText लायेगा। अपने शरीर टैग के अंत में इस पंक्ति को लिखें। या एक तरीका है जिसके शरीर ऑनलोड

0
<!--This code will work --> 
<html> 
<head>  
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" lang='text/javascript' />--> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 

//alert($('#example p').html()); 
alert($('#example').text()); 

alert($('#example #1').text()); 
alert($('#example #2').text()); 
alert($('#example #3').text()); 
alert($('#example #4').text()); 
//alert($("#example p").get().innerHTML); 
//alert('hey'); 


}); 
</script> 
</head> 
<body> 
<div id="example"> 
    <p>...some text...</p> 
    <div id="1"> 
    Here's 1 
    </div> 
    <div id="2"> 
    Here's 2 
    </div> 
    <div id="3"> 
    Here's 3 
    </div> 
    <div id="4"> 
    Here's 4 
    </div> 

</div> 

</body> 
</html> 
+0

आप अपने समाधान में कुछ विवरण जोड़ना चाह सकते हैं ताकि आप यह समझाने के लिए कि आप क्या कर रहे हैं और क्यों। – Sgoettschkes

0
menuItem.firstChild.innerHTML 

शायद यह मदद करता है के बाद बुलाया जाएगा में यह कहते हैं।

0

यह मेरे लिए काम करता है:। $ ("# उदाहरण") सामग्री()

0

मेरे मामले में मैं एक वेब सेवा से वापस एक html स्निपेट हो रही है (जैसा कि एक वस्तु में एक संपत्ति JSON के रूप में लौट आए) । मैं बिना स्वरूपण के, और जावास्क्रिप्ट ब्लॉक के बिना इसे प्रदर्शित करना चाहता हूं। .html() मेरे लिए जावास्क्रिप्ट ब्लॉक लौटाता है, इसलिए यह काम नहीं करता है।

क्या मैं के साथ समाप्त हो गया था:

// Build up the message a bit artificially for this demo. 
// is actually response from ajax call 
var message = '<scri'; 
message += 'pt type="text/javascript">var foo = "bar";</scr' 
message += 'ipt><h2>A heading</h2><div>This is a message</div>'; 

var message_html = jQuery('<div/>') // create a container 
    .html(message) // set the contents from the message 
    .find('script') // find script blocks 
    .remove() // and remove them 
    .end(); // back to the original collection but without script blocks 
// Now get just the text from there 
alert(message_html.text()); 

Working fiddle

संबंधित मुद्दे