jQuery:$(element).text()不起作用 [英] jQuery: $(element).text() doesn't work
本文介绍了jQuery:$(element).text()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我动态生成内容 - 包含链接的div。链接应该弹出一个包含链接文本的单击(showMyText函数)。相反,我得到一个空字符串:($ / b>
为什么不能正常工作?我搜索了Stackoverflow和jQuery API,它应该可以正常工作。
function a(){
var div = document.createElement(div);
div.innerHTML ='< a class =aClasshref =javascript:showMyText(this)>链接文本< / a>';
var parent_div = document.getElementById('dinamicni_div');
parent_div .appendChild(div);
}
函数showMyText(link){
var txt = $(link).text();
alert(txt);
解决方案
获得 text()
为什么不把它用于其他所有东西?
function a(){
var $ div = $(< div>< / div>);
var $ a = $(< a>< / a> )
.attr(href,#)
.addClass(aClass)
.text(链接文本)
.appendTo($ div) ;
$ div.appendTo(#dinamicni_div); $($)
$(#dinamicni_div)。on('click','.aClass',function(){
alert($(this).text() );
});
I am dynamically generating content - div's with links in them. Link should bring up a popup containing link's text when clicked (showMyText function). Instead I get an empty string :(
Why isn't this working? I've searched Stackoverflow and jQuery API and it should work.
function a(){
var div=document.createElement("div");
div.innerHTML='<a class="aClass" href="javascript:showMyText(this)">Link Text</a>';
var parent_div=document.getElementById('dinamicni_div');
parent_div.appendChild(div);
}
function showMyText(link){
var txt=$(link).text();
alert(txt);
}
解决方案
If you're using jQuery to get the text()
why not use it for everything else too?
function a() {
var $div = $("<div></div>");
var $a = $("<a></a>")
.attr("href", "#")
.addClass("aClass")
.text("Link text")
.appendTo($div);
$div.appendTo("#dinamicni_div");
}
$("#dinamicni_div").on('click', '.aClass', function() {
alert($(this).text());
});
这篇关于jQuery:$(element).text()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文