简体   繁体   English

使用javascript和或jquery通过类选择器将html加载到DIV中

[英]loading html into a DIV by class selector using javascript and or Jquery

I currently have a div element as below in my html: 我目前在html中有一个div元素,如下所示:

<div class="tile-group six">

I am looking to use this javascript somehow to load html into the div above 我想以某种方式使用此javascript将html加载到上面的div

<script>          
 $(".tile").click(function(e) {
    $(".tile").toggleClass("flipOutX"); 
   $("tile-group six").load("musability-musictherapy-company-overview.html");
 });    
</script>

It is the second part of the function (the first part does some effect that is currently working so added the line below with the .load bit). 这是函数的第二部分(第一部分当前正在起作用,因此在下面的行中添加了.load位)。

This works for a div id but not for a class. 这适用于div ID,但不适用于类。 Is this possible? 这可能吗?

I NOW HAVE IT WORKING BUT IT HAPPENS TOOO QUICKLY SO I HAVE BEEN ADVISED THE FOLLOWING.... 我现在可以正常工作,但是很快就发生了,因此我已经通知了以下情况。

$(".tile").click(function(e) {
  $(".tile").toggleClass("flipOutX").promise().done(function(){
    $(".tile-group .six").load("musability-musictherapy-company-overview.html");
});

But that does nothing and wont let the function run.??? 但这无济于事,不会让函数运行。

THE CSS for the flipOutX function flipOutX函数的CSS

@-webkit-keyframes flipOutX {
  0% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }

  30% {
    -webkit-transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
    transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
    opacity: 1;
  }

  100% {
    -webkit-transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
    transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
    opacity: 0;
  }
}

@keyframes flipOutX {
  0% {
    -webkit-transform: perspective(400px);
    transform: perspective(400px);
  }

  30% {
    -webkit-transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
    transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
    opacity: 1;
  }

  100% {
    -webkit-transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
    transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
    opacity: 0;
  }
}

.flipOutX {
  -webkit-animation-name: flipOutX;
  animation-name: flipOutX;
  -webkit-backface-visibility: visible !important;
  backface-visibility: visible !important;
}

Seems like you are missing class selector. 好像您缺少类选择器。 Change 更改

$("tile-group six").load("musability-musictherapy-company-overview.html");

to

$(".tile-group .six").load("musability-musictherapy-company-overview.html");

UPDATE: 更新:

If you want to perform the load action when the toggleClass is completed, you can use promises: 如果要在toggleClass完成后执行load操作,则可以使用promises:

$(".tile").toggleClass("flipOutX").promise().done(function(){
    $(".tile-group .six").load("musability-musictherapy-company-overview.html");
});

Notice the . 请注意. . It signifies a class is used as selector . 它表示一个类用作选择器 So if you want to select an element (or many) with the class(es) tile-group six : 因此,如果您要选择一个(或多个)类别为tile-group six的元素(或多个元素):

$(".tile-group .six").load("musability-musictherapy-company-overview.html");

If you can, it would probably be better practice to use an id as selector : 如果可以的话,最好使用id作为选择器

<div id="myDiv" class="whatever"></div>

$('#myDiv').load();

The reason being that you can have many classes named "title-group six" (doesn't have to be unique), and that would load your content into all of them. 原因是您可以拥有许多名为“标题组六”的类(不必是唯一的),这会将您的内容加载到所有这些类中。

Take a look at the Jquery class selector https://api.jquery.com/class-selector/ 看看Jquery类选择器https://api.jquery.com/class-selector/

So you can use: 因此,您可以使用:

the Jquery selector: $( ".myClass .myChildClass" ) jQuery选择器: $( ".myClass .myChildClass" )

the JavaScript selector like this: document.getElementsByClassName('myclass').getElementsByClassName('myChildclass') to find all the 'myChildclass' who have 'myclass' as parent class in your DOM 这样的JavaScript选择器: document.getElementsByClassName('myclass').getElementsByClassName('myChildclass')来查找所有以“ myclass”作为父类的“ myChildclass”

你能试一下吗

$(this).closest("div").load("musability-musictherapy-company-overview.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM