簡體   English   中英

jQuery獲取不是獲取元素的html

[英]jQuery getting isn't getting the html for an element

我正在使用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>'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM