开发者

How to write unit test to assert the value of a jQuery element

I am using jQuery 1.4.1 and here is a simple code.

buildCol1: function() {
  var col = $('<select />', {className: 'col1' }).append($('<option />'));
  return $('<td />').append(col);
},

I am using qunit . In this case the method call is returning a jquery element. Since I am writing a test I should have something like

s = "<td><select class='col1'><option></option></select></td>"

I am not sure how do I get the fully expanded text value from a j开发者_如何学JAVAquery element. I am sure I can get that by fully treaversing all the children. But I am hoping that there is an easier way to unit test this method.

Update:

After I posted the question I got an idea that on the returned element I can check for class name and for the right count of children. Basically this will be like inspecting the returned td to have right values.


So, you want to get the plain HTML source of a jQuery selection? The solution is to put it into a containing element and get that element's inner HTML:

var $myEl = $('<select />', {className: 'col1' }).append($('<option />'));
var src = $("<div></div>").append($myEl).html();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜