禁用元素/类/ id来追加?

时间:2017-05-24 10:00:49

标签: javascript jquery

有没有办法可以禁用某个要附加的元素/类/ id?

<div class="someHtmlElementHereWillAppendOnPageLoad">
   -- but it dont want this part to be appended --
</div>

1 个答案:

答案 0 :(得分:0)

当然 - 将属性canAppend="no"添加到元素,然后使用属性选择器,如下所示:

&#13;
&#13;
$(document).ready(function() {


  /* add the attribute selector [canAppend!="no"]  to your select or */
  $("div[canAppend!='no']").append(" appended content here");
});
&#13;
div { border: 2px solid black }
&#13;
<!-- regular divs that dont have the noAppend attribute -->

<div>div 1</div>

<div> div 2</div>

<!-- special div that has the noAppend attribute -->

<div canAppend='no'>div 3 no append </div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

修改

由于您不知道何时会进行追加操作,只需执行以下操作:

使用jQuery获取具有特定类的div的所有值,并使用encodeURIComponent()$("").attr()

将这些值存储到atttribute中

这使得计时器不断用所需的HTML替换div的HTML

&#13;
&#13;
$(document).ready(function() {
  $(".someHtmlElementHereWillAppendOnPageLoad").each(function(i, ele) {
    $(ele).attr("lockedContents", encodeURI($(ele).html()))
  });

  setInterval(function() {
    $(".someHtmlElementHereWillAppendOnPageLoad").each(function(i, ele) {
      $(ele).html(decodeURIComponent($(ele).attr("lockedContents")));
    })
  }, 100);

});
&#13;
div {
  border: 1px solid black
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="someotherdiv">#1 dont care if it gets edited</div>
<br>

<div class="someHtmlElementHereWillAppendOnPageLoad">
  -- but it dont want this part to be appended --
</div>

<br>
<button value="append to all divs" onclick="$('div').append(' appended text ');">Append to all divs </button>
&#13;
&#13;
&#13;