检查div是否确定html内容然后用新的html替换

时间:2017-04-09 06:37:46

标签: javascript html

<div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
 No items found.
</div>

我知道这可能很简单,我对JS很糟糕。我想查看上面的div,如果它有'找不到物品'。我希望将其替换为“我们目前没有空缺职位,如果您想加入我们的团队,请填写表格表格”

5 个答案:

答案 0 :(得分:1)

(function() {
  // Store accordion in a variable because we're using it multiple times
  var accordion = document.getElementById("accordion");

  // Trim any excess white space
  var content = accordion.innerHTML.trim();

  var old_message = "No items found.";
  var new_message = "We currently have no positions vacant however, please complete an expression of interest form if you would like to join our team";
  
  if (content === old_message) {
    accordion.innerHTML = new_message;
  }
}());
<div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
 No items found.
</div>

答案 1 :(得分:0)

var aDiv = document.getElementById("accordion");
if (aDiv.innerHTML = " No items found.")
   aDiv.innerHTML = "We currently have no positions vacant however, please complete an expression of interest form if you would like to join our team";

答案 2 :(得分:0)

var toReplace = 'No items found';
var replaceWith = 'We currently have no positions vacant however, please complete an expression of interest form if you would like to join our team';

var div = document.getElementById('accordion');
if (-1 !== div.innerHTML.indexOf(toReplace)) { // -1 if match not found, else >= 0
  div.innerHTML = replaceWith;
}
<div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
  No items found.
</div>

答案 3 :(得分:0)

如果你使用的是jQuery,你可以这样做:

var $ = jQuery.noConflict();

var accordion = $('#accordion');
if(accordion.html() == 'No items found.'){
  accordion.html('We currently have no positions vacant however, please complete an expression of interest form if you would like to join our team');
}

答案 4 :(得分:0)

嘿Jake我自己很年轻但是我觉得如果你尝试使用console.log,下面的内容是:= document.getElementById(“accordion”)。innerHTML应该这样做我会立刻自己测试一下