LoginSignup
1
1

More than 5 years have passed since last update.

jQuery#text() の childNodes 互換性問題

Posted at

jQuery#text() にnullや空文字を渡した時のふるまいについて、
jQuery APIレベルでは互換性は保たれているのだけれどDOMのchildNodesについて差分が出る。

https://api.jquery.com/text/
https://developer.mozilla.org/ja/docs/Web/API/Node/childNodes

jQuery_1.11
jQuery('<span>').text('').children()
// []
jQuery('<span>').text('')[0].childNodes.length
// 1
jQuery_2.1
jQuery('<span>').text('').children()
// []
jQuery('<span>').text('')[0].childNodes.length
// 0

これはjQueryを通さずにDOMを使っているツール(今回はCapybara)でjQuery 1.x -> 2.xへの移行をしている最中に
期待した動作をしなかったケースがあって気付いた。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1