元素出现在DOM中后运行Function

时间:2016-04-12 12:35:08

标签: jquery

如何在元素加载到DOM后从元素中删除它?

以下情况:

如果DOM中出现类名为selected的元素,我想从中删除类highlight

示例:

<div class="selected highlight">
<div class="group">Material</div>
<div class="option">350g Chromo-Sulfatkarton matt</div>
</div>

4 个答案:

答案 0 :(得分:1)

每个答案都使用jQuery。在我看来,CSS在这里更具相关性。

我使用CSS keyframesanimation制作了此代码段。

&#13;
&#13;
function create() {
  var element = $("<p class='selected highlight'>" + Math.random() + "</p>");
  $("#container").prepend(element);
}
&#13;
.selected {
  -webkit-animation: highlight 1s infinite;
  -moz-animation: highlight 1s;
  -o-animation: highlight 1s;
  animation: highlight 1s;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes highlight {
  from {
    background-color: red;
  }
  to {
    background-color: none;
  }
}

/* Standard syntax */
@keyframes highlight {
  from {
    background-color: red;
  }
  to {
    background-color: none;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button onclick='create()'>Create an element</button>
<div id="container"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据您是否使用页面加载元素,在这种情况下您将侦听DOM ready event或其他一些事件,在适当的事件发生时应触发以下代码:

$('.selected.highlight').removeClass('highlight');

如果正在收听 DOM ready事件,则代码为:

$(function() {
    $('.selected.highlight').removeClass('highlight');
});

答案 2 :(得分:0)

var path = cordova.file.applicationDirectory + "www/media/video.mp4";
if (cordova.platformId == 'ios') {
    var options = {
        successCallback: function () {
            console.log("Video was closed without error.");
        },
        errorCallback: function (errMsg) {
            console.log("Error! " + errMsg);
        },
        orientation: 'landscape'
    };
    window.plugins.streamingMedia.playVideo(path, options);
}

答案 3 :(得分:0)

我认为您可以使用jquery的DOMNodeInserted插入事件,如下所示

$('body').on('DOMNodeInserted', function(e) {
  if ($(e.target).hasClass('selected')) {
    $(e.target).removeClass('highlight')
  }
});