简体   繁体   English

使用Javascript的动态HTML:用不同的内容填充相同的HTML对象

[英]Dynamic HTML using Javascript: Fill same HTML object with different contents

This is the general question: 这是一个普遍的问题:
Which is the best solution when there is an HTML object to fill in different ways depending on DOM events? 当有一个HTML对象根据DOM事件以不同的方式填充时,哪一种是最佳解决方案?

This is my case in particular: 我的情况尤其如此:
I have a navbar where each button shows a modal. 我有一个导航栏,其中每个按钮都显示一个模式。 The skeleton of the modal is always the same but, as you can imagine, the content change depending on the button. 模态的框架始终是相同的,但是,正如您可以想象的那样,内容根据按钮而变化。
Then I thought to make each button fill the modal in a different way using Javascript and onclick attributes, this is more or less is how it looks like: 然后,我想使用Javascript和onclick属性使每个按钮以不同的方式填充模式,这或多或少是这样的:

<li><a data-toggle="modal" data-target="#myModal" id="button1" onclick="fillmodal(this.id)">Text button1</a></li>
<li><a data-toggle="modal" data-target="#myModal" id="button2" onclick="fillmodal(this.id)">Text button2</a></li>
<li><a data-toggle="modal" data-target="#myModal" id="button3" onclick="fillmodal(this.id)">Text button3</a></li>

Let's imagine for example that: 让我们想象一下:

  • Button1 has to show just a text with some imagines inside the modal. Button1必须只显示模态内部带有一些想象的文本。
  • Button2 has to show a form with a text area and a submit button inside the modal. Button2必须显示一个表格,该表格在模式中具有一个文本区域和一个提交按钮。
  • Button3 has to show the same form of Button2 with 2 more fields. Button3必须显示Button2的相同形式,还有2个字段。

As you can see the content is always different but some element could be shared (this is the case of the text area showed by Button2 and Button3 in our example). 如您所见,内容总是不同的,但是可以共享某些元素(在我们的示例中,Button2和Button3所显示的文本区域就是这种情况)。

There are many different approaches to this problem: 有很多解决此问题的方法:

I could write a totally empty modal in the HTML and the function fillmodal(elementId) could make all the work to fill it, but I find this solution quite ugly because it takes a lot of lines in the script to fill the modal, then another solution would be to use AJAX Requests in order to take the content directly from the server. 我可以在HTML中编写一个完全空的模态,并且函数fillmodal(elementId)可以完成所有工作,但是我发现此解决方案非常难看,因为脚本中需要很多行来填充模态,然后再填充解决方案是使用AJAX请求,以便直接从服务器获取内容。
I think this second solution would be the best in case of a lot of buttons. 我认为如果有很多按钮,第二种解决方案将是最好的。

So far the best solution that I found consist in to write everything directly inside the modal as hidden elements, and make the function fillmodal(elementId) show what necessary just changing the attributes. 到目前为止,我发现的最佳解决方案是将所有内容直接写在模态内部作为hidden元素,并使函数fillmodal(elementId)显示更改属性的必要条件。

Which do you think is the best solution? 您认为哪个是最佳解决方案?

Lots of different solutions for what you are trying to achieve. 针对您要实现的目标,有许多不同的解决方案。

Here's one (one modal per button): 这是一个(每个按钮一个模式):

<li><a id="button1" onclick="showmodal(this.id)">Text button1</a></li>
<li><a id="button2" onclick="showmodal(this.id)">Text button2</a></li>
<li><a id="button3" onclick="showmodal(this.id)">Text button3</a></li>

<div style="display: none;" class="modal" id="button1_content">Content 1</div>
<div style="display: none;" class="modal" id="button2_content">Content 2</div>
<div style="display: none;" class="modal" id="button3_content">Content 3</div>

function showmodal(id) {
    document.getElementById(id+'_content').style.display = "block";
}

Here's another (get modal's html from another div): 这是另一个(从另一个div获取模态的html):

<li><a id="button1" onclick="showmodal(this.id)">Text button1</a></li>
<li><a id="button2" onclick="showmodal(this.id)">Text button2</a></li>
<li><a id="button3" onclick="showmodal(this.id)">Text button3</a></li>

<div style="display: none;" id="button1_content">Content 1</div>
<div style="display: none;" id="button2_content">Content 2</div>
<div style="display: none;" id="button3_content">Content 3</div>

<div id="modal">Content 3</div>

function showmodal(id) {
    document.getElementById('modal').innerHTML =
      document.getElementById(id+'_content').innerHTML;
}

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

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