Symbole wieloznaczne w selektorach jQuery

Próbuję użyć symboli wieloznacznych, aby uzyskać id wszystkich elementów, których id zaczyna się od "jender". Próbowałem $('#jander*'), $('#jander%') ale to nie działa..

Wiem, że mogę użyć klas elementów, aby to rozwiązać, ale jest to również możliwe za pomocą symboli wieloznacznych??

<script type="text/javascript">

  var prueba = [];

  $('#jander').each(function () {
    prueba.push($(this).attr('id'));
  });

  alert(prueba);


});

</script>

<div id="jander1"></div>
<div id="jander2"></div>
Author: UpHelix, 2011-03-21

6 answers

Aby uzyskać wszystkie elementy zaczynające się od "jender" należy użyć:

$("[id^=jander]")
To get those that end with "jender"
$("[id$=jander]")

Zobacz także jQuery documentation

 1120
Author: nico,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-08 17:47:17

Ponieważ tytuł sugeruje symbol wieloznaczny, możesz również użyć tego:

$(document).ready(function(){
  console.log($('[id*=ander]'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="jander1"></div>
<div id="jander2"></div>

Spowoduje wybranie podanego ciągu w dowolnym miejscu id.

 106
Author: Martijn Smidt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-07 09:10:46

Spróbuj jQuery zaczyna się od

Selektor,'^=', np

[id^="jander"]

Muszę jednak zapytać, dlaczego nie chcesz tego zrobić za pomocą klas?

 35
Author: GoatInTheMachine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-09 13:24:36

Do zajęć można użyć:

div[class^="jander"]
 33
Author: l3thal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-25 13:55:04

Do uzyskaj id z dopasowania wildcard:

$('[id^=pick_]').click(
  function(event) {

    // Do something with the id # here: 
    alert('Picked: '+ event.target.id.slice(5));

  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pick_1">moo1</div>
<div id="pick_2">moo2</div>
<div id="pick_3">moo3</div>
 12
Author: PJ Brunet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-09 13:25:56

Jeśli masz bardziej złożony łańcuch id, podwójne cudzysłowy są obowiązkowe.

Na przykład, jeśli masz id takie jak: id="2.2", poprawnym sposobem dostępu do niego jest: $('input[id="2.2"]')

W miarę możliwości używaj podwójnych cudzysłowów, ze względów bezpieczeństwa.

 9
Author: eduard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-29 09:16:28