JavaScript-在文本手风琴中不显示标题

时间:2018-10-19 16:27:24

标签: javascript jquery html node.js

首先,请参阅屏幕截图,这里我正在使用手风琴文本编辑器,您可以在其中添加标题和正文。 enter image description here

当我多次单击“添加”按钮,然后显示这种类型的内容时。不显示标题文字。我觉得这是JS文件的问题。有什么解决办法吗? 预先感谢!

enter image description here

$(document).ready(function() {
	$('#Add_btn').click(function(){
		$('#accordion_body').append($('#editableDiv').html());    		
		var x = document.getElementById("accordion_input").value;
		document.getElementById("accordion_title").innerHTML = x;
	});    	
});

function myAccorFunction() {
    var Myaccordion = `
        <div class="Myacc" id="acc_main" contenteditable="false"
             class="accordion-main"
             style="position: relative;width: 98.5%; left: 9px;">
        </div>
        <div class="panel-group trash_removed" id="accordion" role="tablist"
             aria-multiselectable="true"
             style="margin-bottom: 7px;margin-top: 10px"
             contenteditable="false">
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingOne"
                 contenteditable="false">
              <h4 class="panel-title">
                <a id="accordion_title" role="button" data-toggle="collapse"
                   data-parent="#accordion" href="#expandingCol"
                   aria-expanded="false" aria-controls="collapseOne">
                </a><div class="icon_area">
                <i class="fa fa-edit" data-toggle="collapse"
                   data-parent="#accordion" href="#collapseOne"
                   aria-expanded="false" aria-controls="collapseOne">
                </i>
                <i onclick="myfundelete()" class="fa fa-trash">
                </i>
              </div>
            </h4>
          </div>
          <div id="expandingCol" class="panel-collapse collapse"
               role="tabpanel" aria-labelledby="headingOne"> 
            <div id="accordion_body" class="panel-body" >
            </div>
          </div>
        </div>
      </div>
    `;
    $("#editor").append(Myaccordion);
}
<button onclick="myAccorFunction()" id="Add_btn"
        class="btn btn-primary note-image-btn" role="button"
        data-toggle="collapse" data-parent="#accordion"
        href="#collapseOne" aria-expanded="true"
        aria-controls="collapseOne">Add</button>

1 个答案:

答案 0 :(得分:1)

似乎您多次使用同一元素ID #accordion_title。当浏览器假定element ID is unique within a page时,document.getElementById("accordion_title").innerHTML = x;只会更改具有该ID的第一个元素的innerHTML,而不会更改由ADD按钮附加的后继元素。

尝试为标题文本元素使用类名而不是ID,然后遍历该类的每个元素以更改其innerHTML。

$('.accordion_title').each(function() {
    $(this).html($('#accordion_input').val());
});