में बच्चों के बिना किसी तत्व का टेक्स्ट प्राप्त करें मैं बच्चों के बिना तत्व का पाठ कैसे प्राप्त करूं? न तो element.textContent
और न ही element.innerText
काम कर रहे प्रतीत होते हैं।जावास्क्रिप्ट
HTML:
jQuery.fn.justtext = function(text) {
return $(this).clone()
.children()
.remove()
.end()
.text();
};
function fool(el) {
reverse(el);
function reverse(el) {
$(el).children().each(function() {
if($(this).children().length > 0) {
reverse(this);
if($(this).justtext() != "")
reverseText(this);
} else {
reverseText(this)
}
});
}
function reverseText(el){
var text = el.textContent;
var frag = text.toString().split(/ /);
var foo = "";
var punctation_marks = [".",",","?","!"," ",":",";"];
for(i in frag){
if(punctation_marks.indexOf(frag[i]) == -1)
foo += actualReverse(frag[i],punctation_marks) + " ";
}
el.textContent = foo;
}
function actualReverse(text,punctation_marks) {
return (punctation_marks.indexOf(text.split("")[text.split("").length-1]) != -1)?text.split("").slice(0,text.split("").length-1).reverse().join("") + text.split("")[text.split("").length-1] : text.split("").reverse().join("");
}
}
संपादित:
<body>
<h1>Test Heading</h1>
<div>
Awesome video and music. Thumbs way up. Love it. Happy weekend to you and your family. Love, Sasha
</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
fool("body");
</script>
और यहाँ fool
समारोह है node.nodeType
का उपयोग कर वास्तव में मदद नहीं करता है और यहाँ है क्यों: Imaginge निम्न HTML
<td class="gensmall">
Last visit was: Sat Mar 31, 2012 10:50 am
<br>
<a href="./search.php?search_id=unanswered">View unanswered posts</a> | <a href="./search.php?search_id=active_topics">View active topics</a>
</td>
अगर मैं nodeType
का उपयोग करेंगे, केवल a
तत्व का टेक्स्ट बदल जाएगा, लेकिन नहीं td
ही ("अंतिम यात्रा ....")
plz अपने html – Jigs
किसी भी कोड पोस्ट? आपका चयनकर्ता शायद गलत है, 'तत्व' किसी भी सामग्री को प्राप्त नहीं करता है जब '.textContent' और' innerText' खाली होते हैं। –
"बच्चों के बिना" आपका क्या मतलब है? – Pointy