简体   繁体   English

innerHTML在Javascript onClick中未更改

[英]innerHTML Not Changing in Javascript onClick

I have a default page which has bootstrap tabs . 我有一个默认页面,其中包含bootstrap tabs When i click one of the tabs the content loads from the relevant DIV but when i click the other tab it does not replace the other Div . 当我单击一个选项卡时,内容将从相关的DIV加载,但是当我单击另一个tab它不会替换另一个Div

All code below 下面的所有代码

Default HTML 预设HTML

 function load_RbCode() { document.getElementById("rbCode").innerHTML = '<object type="text/html" data="functionality.html" style="width: 100%; height: -webkit-fill-available"></object>'; } function load_HelpModalCode() { document.getElementById("helpModalCode").innerHTML = '<object type="text/html" data="help_modal.html" style="width: 100%; height: -webkit-fill-available"></object>'; } 
 <div> <ul class="nav nav-tabs"> <li class="nav-item"> <a id="rbCodeMenu" class="nav-link" href="#" onclick="load_RbCode()">Rule Builder Code</a> </li> <li class="nav-item"> <a id="helpModalCodeMenu" class="nav-link" href="#" onclick="load_HelpModalCode()">Help Modal</a> </li> </ul> <div id="rbCode"></div> <div id="helpModalCode"></div> </div> 

As i said it goes to the correct file and displays. 正如我所说,它会转到正确的文件并显示。 If i click the 'Help' tab first then the 'Rule' tab it does replace but i think this is because there is a lot more HTML in the 'Rule' file but if i select 'Rule' tab first then the 'Rule' HTML still displays and it should just be a button which launchs a Modal 如果我先单击“帮助” tab那么它会替换“规则” tab ,但是我认为这是因为“规则”文件中有很多HTML ,但是如果我先选择“规则” tab则选择“规则” HTML仍然显示,它应该只是一个button Modalbutton

Prevent the default behavior of the anchor tag 防止锚标记的默认行为

 function load_RbCode(e) { e.preventDefault(); document.getElementById("rbCode").innerHTML = '<object type="text/html" data="functionality.html" style="width: 100%; background:red;height: -100%"></object>'; } function load_HelpModalCode(e) { e.preventDefault(); document.getElementById("helpModalCode").innerHTML = '<object type="text/html" data="help_modal.html" style="width: 100%;background:green; height: 100%"></object>'; } 
 <div> <ul class="nav nav-tabs"> <li class="nav-item"> <a id="rbCodeMenu" class="nav-link" href="#" onclick="load_RbCode(event)">Rule Builder Code</a> </li> <li class="nav-item"> <a id="helpModalCodeMenu" class="nav-link" href="#" onclick="load_HelpModalCode(event)">Help Modal</a> </li> </ul> <div id="rbCode"></div> <div id="helpModalCode"></div> </div> 

Your code only displays the information when it is clicked but it does not hide the other tab content (or clear the content) when a tab is clicked. 您的代码仅在单击时显示信息,而在单击选项卡时不会隐藏其他选项卡内容(或清除内容)。

Edit: same as what was mentioned by ADyson in the comment 编辑:与ADyson在评论中提到的内容相同

function load_RbCode() {
    //clear the other tab content
    document.getElementById("helpModalCode").innerHTML = "";
    document.getElementById("rbCode").innerHTML='<object type="text/html" data="functionality.html" style="width: 100%; height: -webkit-fill-available"></object>';
}

function load_HelpModalCode() {
    //clear the other tab content
    document.getElementById("rbCode").innerHTML = "";
    document.getElementById("helpModalCode").innerHTML='<object type="text/html" data="help_modal.html" style="width: 100%; height: -webkit-fill-available"></object>';
}

You need to clear the content of the div element which you are not using. 您需要清除未使用的div元素的内容。

This isn't related to your issue but when you feed the innerHTML() method a string of text containing HTML markup it is converted to a DocumentFragment object representing the new DOM nodes for the HTML elements. 这与您的问题无关,但是当您向innerHTML()方法提供包含HTML标记的文本字符串时,它将转换为一个DocumentFragment对象,该对象代表HTML元素的新DOM节点。 You can do this yourself in JavaScript and when you bundle the code into a function you have cleaner and more performant code. 您可以使用JavaScript自己执行此操作,并且将代码捆绑到一个函数中时,您会获得更简洁,性能更高的代码。

 function addObjectElement (containerElem, contentUrl) { // Create a new `object` element var newElement = document.createElement("object"); // Set the element attributes newElement.setAttribute("type", "text/html"); newElement.style.cssText = "width: 100%; height: -webkit-fill-available"; newElement.setAttribute("data", contentUrl); // Clear the container element if it's populated if (containerElem.hasChildNodes()) containerElem.innerHTML = ""; // Add the new element to the container containerElem.appendChild(newElement); } function load_RbCode(event) { event.preventDefault(); // Clear `#helpModalCode` document.getElementById("helpModalCode").innerHTML = ""; // Populate `#rbCode` addObjectElement(document.getElementById("rbCode"), "functionality.html"); } function load_HelpModalCode(event) { event.preventDefault(); // Clear `#rbCode` document.getElementById("rbCode").innerHTML = ""; // Populate `#helpModalCode` addObjectElement(document.getElementById("helpModalCode"), "help_modal.html"); } 
 <div> <ul class="nav nav-tabs"> <li class="nav-item"> <a id="rbCodeMenu" class="nav-link" href ="#" onclick="load_RbCode(event)">Rule Builder Code</a> </li> <li class="nav-item"> <a id="helpModalCodeMenu" class="nav-link" href ="#" onclick="load_HelpModalCode(event)">Help Modal</a> </li> </ul> <div id="rbCode"></div> <div id="helpModalCode"></div> </div> 

Use this below code: your option tag is blank.. so its not showing.. when you put some value in it.. your code also show this. 请在下面的代码中使用此代码:您的option标记为空白..因此,当您在其中添加一些值时,它不会显示..您的代码也显示此。

  function load_RbCode() { document.getElementById("rbCode").innerHTML='<object type="text/html" data="functionality.html" style="width: 100%; height: -webkit-fill-available;background:red;">one</object>'; } function load_HelpModalCode() { document.getElementById("helpModalCode").innerHTML='<object type="text/html" data="help_modal.html" style="width: 100%; height: -webkit-fill-available;background:blue;">two</object>'; } 
  <div> <ul class="nav nav-tabs"> <li class="nav-item"> <a id="rbCodeMenu" class="nav-link" href ="#" onclick="load_RbCode()">Rule Builder Code</a> </li> <li class="nav-item"> <a id="helpModalCodeMenu" class="nav-link" href ="#" onclick="load_HelpModalCode()">Help Modal</a> </li> </ul> <div id="rbCode">text</div> <div id="helpModalCode">tst2</div> </div> 

Try Jquery. 试试Jquery。

Add <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> and then $('#rbCode').html('<object type="text/html" data="functionality.html" style="width: 100%; height: -webkit-fill-available"></object>'); 添加<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> ,然后添加$('#rbCode').html('<object type="text/html" data="functionality.html" style="width: 100%; height: -webkit-fill-available"></object>');

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

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