简体   繁体   English

jQuery获取不是获取元素的html

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

I am using Jasmine to text and setting fixtures. 我正在使用Jasmine来发送文字和设置固定装置。

I have this following test code here which demonstrates a problem: 我在这里有下面的测试代码,它演示了一个问题:

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');
   });
});

Ignoring what the test does itself, look at what I am trying to append to the body tag. 忽略测试本身会做什么,请查看我要附加到body标签的内容。 What I want is the actual html element to be appended, and for whatever reason I getting back an html object. 我想要的是要附加的实际html元素,并且无论出于什么原因,我都返回了html对象。

Please see the fiddle: http://jsfiddle.net/sidouglas/e6jau1mq/9/ 请参阅小提琴: http : //jsfiddle.net/sidouglas/e6jau1mq/9/

Is this a Jasmine issue or a PEBKAC issue? 这是茉莉花问题还是PEBKAC问题?

Thank you Simon. 谢谢西蒙。

.append takes the actual HTML string, so just use that instead of the jQ object: .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