Obtain item with point in class name with JQuery

13

It turns out that following the documentation of the library fancybox I find that to define the behavior of its elements through classes, a syntax must be followed like the following:

<a href="http://www.dominio-ejemplo.mx/pagina" class="fancybox.iframe">
    <!-- Código HTML -->
</a>

I want to add " x " behaviors through jQuery to that type of elements, but I find that even escaping the character of "." (period) with the backslash () does not take the element.

I leave the following snippet:

$(function(){
  $('button').on('click', function(){
    /* No funciona */
    $('.fancybox\.image').remove();
  });
});
.fancybox\.iframe {
  color: white;
  background-color: red;
}

.fancybox\.image {
  color: white;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>
    <a href="http://www.dominio-ejemplo.mx/pagina" class="fancybox.iframe">Estilo iframe</a>
  </li>
  <li>
    <a href="http://www.dominio-ejemplo.mx/pagina" class="fancybox.image">Estilo imagen</a>
  </li>
</ul>

<button>Borrar: fancybox.iframe</button>
    
asked by Chofoteddy 15.12.2015 в 02:14
source

3 answers

13

The problem is here (in the slash):

$('.fancybox\.image').remove();

Change it by .fancybox\.image with double slash.

The rest of the code is fine, what happens is that the string '\.' in JavaScript becomes just '.' because \ is the escape character, and what you want is that the selector receives \. .

    
answered by 15.12.2015 / 02:29
source
8

Only enclose in single or double quotes, depending on which you use to define the selector , the value of the attribute class . This is:

$("[class='fancybox.iframe']").remove();

$(function(){
  $('button').on('click', function(){
    /* No funciona */
    // $('.fancybox\.image').remove();
    $("[class='fancybox.iframe']").remove();
  });
});
.fancybox\.iframe {
  color: white;
  background-color: red;
}

.fancybox\.image {
  color: white;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>
    <a href="http://www.dominio-ejemplo.mx/pagina" class="fancybox.iframe">Estilo iframe</a>
  </li>
  <li>
    <a href="http://www.dominio-ejemplo.mx/pagina" class="fancybox.image">Estilo imagen</a>
  </li>
</ul>

<button>Borrar: fancybox.iframe</button>
    
answered by 15.12.2015 в 04:19
1

It turns out that Jquery in version 3.0 has added a new method called escapeSelector. What it allows is to be able to make selections of elements with characters typical of the css selection operators.

Its use would be something like this

$( "#" + $.escapeSelector( "abc.def" ) )

You have more information here link

    
answered by 24.02.2017 в 18:23