jQuery获取是't获取元素的html

jQuery getting isn't getting the html for an element

本文关键字:获取 元素 html jQuery      更新时间:2023-09-26

我正在使用Jasmine发送文本并设置固定装置。

我在这里有以下测试代码,它说明了一个问题:

describe("foo", function() {
var $input = $('<input type="text" name="testfield" value="10" id="testInput">');
var $textArea = $('<textarea name="textarea" id="testTextArea">10</textarea>');
console.log( 'typeof: $input.get(0)' + ' ' + typeof $input.get(0) ); //object
console.log( 'typeof: $input[0]' + ' ' + typeof $input[0] ); //object
console.log( 'typeof: $input[0].toString()' + ' ' + typeof $input[0].toString() ); //string
beforeEach(function(){
    var html =  $input[0] + $textArea[0];
    var ghtml =  $input[0] + $textArea[0];
    $('body').append(html);
    $('body').append(ghtml); 
    $('body').append(ghtml + html );
    $('body').append( $input[0] );
    $('body').append( $textArea[0] );
    $('body').append( $input[0] + '' + $textArea[0]);
    $('body').append( $input[0] + $textArea[0]);
    $('body').append( $input.get(0) + $textArea.get(0) );
    $('body').append( $input.get(0) + ' ' + $textArea.get(0) );
    $('body').append( $input.html() + $textArea.html() );
    $('body').append( $input[0].toString() + $textArea[0].toString() );
    $('body').append( ($input[0].toString()) + ($textArea[0].toString()) );
    $('body').append( ($input.get(0).toString()) + ($textArea.get(0).toString()) );
    $('body').append( ($input.get(0).toString()) + ' ' + ($textArea.get(0).toString()) );

    setFixtures( html );
});  
   it("has a value of bar", function() {
     expect(foo).toBe('bar');
   });
});

忽略测试本身的作用,看看我试图附加到body标记的内容。我想要的是要附加的实际html元素,无论出于什么原因,我都会返回一个html对象。

请看小提琴:http://jsfiddle.net/sidouglas/e6jau1mq/9/

这是茉莉花问题还是PEBKAC问题?

谢谢西蒙。

.append采用实际的HTML字符串,所以只需使用它而不是jQ对象:

var $input = '<input type="text" name="testfield" value="10" id="testInput">'
var $textArea = '<textarea name="textarea" id="testTextArea">10</textarea>'