Effective DOM Selection Methods - Choosing by Attribute Slow?

I use a script so that all inputs have a placeholder in browsers that do not yet support this function.

In this script, I use

$('input[placeholder]').each(function() {

to select all items for action.

I was wondering if this could be slow, since its not a very specific choice, for example

$('#input').each(function() {

which I know, is selected faster (but I do not want to specify all identifiers separately).

You would recommend adding classes to all inputs with placeholder attributes as follows:

$('.iHaveaPlaceholder').each(function() {

to make the choice faster (I think that class selection is faster than attribute selection). But that would incorrectly use the css class assignment just for style, and it would fill the house.

Do you have any suggestions or methods to improve such tasks?

+3
4

JSPerf input[placeholder], .hasPlaceholder input .filter().

, , , , .

'input[placeholder]'? , . , ( ...). , , , , , .hasPlacehoder IE 6, . , .

HTML !

: input.hasPlaceholder ...

+3

DOM, , . "", , .

- DOM. , . - , , . , , - .

+2

, id, $("#theForm input[placeholder]"), , .

0

( ?). , .

0
source

Source: https://habr.com/ru/post/1793997/


All Articles