在Jquery中获取$(this)的Html

在Jquery中获取$(this)的Html,jquery,Jquery,如何在下面的函数中获取$(this)的Html allhdn.each(function (){ var $this = $(this); var newHtml = (str + $this.html()); }); $(this).html()返回空字符串。我认为您存在变量范围问题 var html; var newHtml; allhdn.each(function (){ html = $(this).html(); newHt

如何在下面的函数中获取
$(this)
的Html

allhdn.each(function (){
      var $this = $(this);       
      var newHtml = (str + $this.html());
});

$(this).html()
返回空字符串。

我认为您存在变量范围问题

var html;
var newHtml;
allhdn.each(function (){
    html = $(this).html();  
    newHtml = (str + html);
});

如果它是
输入
您需要
.val()

$(this).html()
获取标记的内部html代码。因为您迭代的标记是输入字段,所以它们没有任何内部标记。要解决此问题,我认为您需要包装一个新标记,并获取该标记的内部html:

//包装在一个封闭的标记中
$this.wrap(“”);
//获取代码
var newHtml=(str+$this.parent().html());

但是正如@Interstellar\u Coder所指出的,你确定你想要HTML,而不仅仅是隐藏输入上的
.val()

你迭代了哪些元素?看起来是正确的。也许元素实际上没有内容?在
allhdn
对象中是什么?什么类型的控件?@星际编码器
$this=$(this)
allHdn是所有隐藏的5n字段。我有这种格式的tds。allHdn是var allhdnMonths=$('input[Id^=“hdnFields”]”);val()不会返回html。输入没有任何html:-P
var newVal = (str + $this.val());
// wrap in an enclosing tag
$this.wrap('<span/>');
// get the code
var newHtml = (str + $this.parent().html());