javascript 在不刷新页面的情况下更改 HTML 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15808348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:10:32  来源:igfitidea点击:

Change HTML code without refreshing the page

javascripthtmlweb-applicationsfrontend

提问by Martin Dvoracek

Is there any possibility how to change HTML(.jsp) page without refreshingit?

有没有可能how to change HTML(.jsp)页面without refreshing呢?

Usecase is that user writes something to textarea then hits some button and I need then to add some object ie picture to previously defined place in my page ~~~> I need to change HTML code without refreshing the page (without the loss of the text in the textbox). I really need to change the HTML code, so pls don't advice any solution without changing the HTML code.

用例是用户向 textarea 写入一些内容然后点击某个按钮,然后我需要添加一些对象,即图片到我页面中先前定义的位置~~~> 我需要更改 HTML 代码而不刷新页面(不丢失文本)在文本框中)。我真的需要更改 HTML 代码,所以请不要在不更改 HTML 代码的情况下建议任何解决方案。

I'm not very into frontend technologies, so I don't know if it's even possible, so excuse me pls, if this is a stupid question.

我不是很喜欢前端技术,所以我不知道它是否可能,所以请原谅我,如果这是一个愚蠢的问题。

I can use only frontend technologies.

我只能使用前端技术。

采纳答案by Jace

Using jQuery(a JavaScript library) you can utilize the load()function to load the contents of another HTML file on your server and place it anywhere you want on the current page without refreshing (so you can even replace the current HTML if you like).

使用jQuery(一个 JavaScript 库),您可以利用该load()函数将另一个 HTML 文件的内容加载到您的服务器上,并将其放置在当前页面上您想要的任何位置而无需刷新(因此您甚至可以根据需要替换当前的 HTML)。

jQuery:
http://jquery.com/

jQuery:http :
//jquery.com/

jQuery load():
http://api.jquery.com/load/

jQuery 加载():http :
//api.jquery.com/load/



Alternative Suggestion:
However, I know you say you can't refresh the page, but, if the only reason is because you need to keep the text in the textbox, you could use a form to POSTthat text to another .jsppage (or even the same .jsppage, depending on how you go about it) where it will be available to you to use at your own discretion (to put it in another textbox, for example).

替代建议:
但是,我知道你说你不能刷新页面,但是,如果唯一的原因是因为你需要将文本保留在文本框中,你可以使用一个表单POST将该文本转移到另一个.jsp页面(甚至同一个.jsp页面,取决于您如何进行),您可以自行决定使用它的位置(例如,将其放在另一个文本框中)。

回答by Kees Sonnema

Maybe this will work for you.

也许这对你有用。

<div id="mydiv"></div>
<a id="refresh">click</a>

<script>
$(function() {
  $("#refresh").click(function() {
     $("#mydiv").load("yourdomain.com/file.php")
  })
})

also see this:

另见:

http://woork.blogspot.nl/2007/10/how-to-change-text-using-javascript.html

http://woork.blogspot.nl/2007/10/how-to-change-text-using-javascript.html

回答by errieman

Use JavaScriptso you can make changes to the Document Object ModelIt is very easy to learn and very powerful.

使用JavaScript以便您可以更改文档对象模型它非常容易学习并且非常强大。

回答by user6163731

You should use the javascript function document.getElementById (DIV THAT YOU WOULD LIKE TO PLACE IN).innerHTML = YOUR_TEXT;.

您应该使用 javascript 函数 document.getElementById (DIV THAT YOU WOULD LIKE TO PLACE IN).innerHTML = YOUR_TEXT;。

If you need a response from the server without reloading the page (you will still have to load, but the client will not notice), try using AJAX.

如果您需要来自服务器的响应而不重新加载页面(您仍然需要加载,但客户端不会注意到),请尝试使用 AJAX。

回答by penguat

Yes, this is possible. You should use JavaScript to change the HTML used to display the page.

是的,这是可能的。您应该使用 JavaScript 来更改用于显示页面的 HTML。

You will probably find it useful to use a library such as JQuery.

您可能会发现使用诸如 JQuery 之类的库很有用。