acceda a $(this) con href=“javascript:…” en jQuery
-
09-06-2019 - |
Pregunta
Estoy usando jQuery.Llamo a una función de JavaScript con el siguiente html:
<li><span><a href="javascript:uncheckEl('tagVO-$id')">$tagname</a></span></li>
me gustaría quitar el li
elemento y pensé que esto sería fácil con el $(this)
objeto.Esta es mi función de JavaScript:
function uncheckEl(id) {
$("#"+id+"").attr("checked","");
$("#"+id+"").parent("li").css("color","black");
$(this).parent("li").remove(); // This is not working
retrieveItems();
}
Pero $(this)
es indefinido.¿Algunas ideas?
Solución
Pruebe algo como esto (p. ej.para ocultar el <li>
):
function unCheckEl(id, ref) {
(...)
$(ref).parent().parent().hide(); // this should be your <li>
}
Y tu enlace:
<a href="javascript:uncheckEl('tagVO-$id', \$(this))">
$(this)
no está presente dentro de su función, porque ¿cómo se supone que sabe desde dónde se llama la acción?No pasas ninguna referencia en él, así que $(this)
podría referirse a todo menos a <a>
.
Otros consejos
¿Por qué no algo como:
<li id="uncheck_tagVO-$id">$tagname</li>
y
$('li').click( function() {
var id = this.id.split("_")[1];
$('#'+id).attr("checked","").parent("li").css("color","black");
$(this).remove();
retrieveItems();
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow