jQuery获取是't获取元素的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>'
相关文章:
- 如何在jQuery中获取元素的形式
- 通过id和class属性获取元素
- Javascript:Can't使用getElementById获取元素
- 使用jquery.html()获取元素本身的html
- 如何从iframe内部获取元素
- Javascript函数获取元素内容
- 使用模板后,使用Angular获取元素属性值
- 获取元素类名的部分
- 从JSON.parse获取元素
- 获取元素内部的缩写
- 在不引用文档的情况下使用AngularJS获取元素
- 如何获取元素's的title属性
- 使用Javascript,获取元素的方法是什么,基于打开和关闭标记之间的文本
- Javascript获取元素背景图像,但消除了“;url()”;
- Javascript:按类获取元素,但跳过某个类
- 当display:none时,getElementById()无法获取元素
- 我的ID获取元素不起作用
- 如何获取元素:使用javascript和CORS悬停背景
- 能够获取元素长度,但不能获取内部 HTML
- 如何使用jQuery在模板kendo UI中获取元素类