Document.write问题 [英] Document.write issues

查看:111
本文介绍了Document.write问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个页面,用户可以回答关于他们正在销售的产品的问题(使用单选按钮)。当他们点击页面底部的按钮时,会弹出价格报价。我写了一个JavaScript函数,当用户点击按钮时执行。它计算价格并使用document.write显示它,但每当用户点击按钮时,它就会打开一个新页面并显示我告诉它的内容。

  function getQuote(){
document.write(Your Quote Is:$,price,.00);
}

这里是按钮的代码:

 < button type =buttononclick ='getQuote()'>显示报价< / button> 

但是当我按下按钮时,会出现一个新页面,它只显示格式我放入 document.write 短语。



我试过使用 .innerHTML document.write 发送到页面的另一部分,但同样的问题仍然存在。

document.write 存在没有问题,它正在做它应该做的事情:


用新内容覆盖页面。


如果不要

想要这样做,那么你必须给它一些上下文来写信。



例如:

  function getQuote(){
var textArea = document.getElementById('textArea');
textArea.innerHTML =您的报价是:$,价格,.00;

$ / code>

它把你的文本放到一个DOM元素中,而 id =textArea


I am making a page where users can answer questions (using radio buttons) about a product they are selling back. When they hit a button at the bottom of the page, a price quote will pop up. I have written a javascript function to be performed when the user hits the button. It calculates the price and displays it using document.write, but whenever the user hits the button, it opens up a new page and displays what I told it to.

function getQuote(){
    document.write("Your Quote Is: $", price, ".00");
}

And here is the code for the button:

<button type="button" onclick='getQuote()'>Display Quote</button>

But when I push the button, a new page shows up and it shows the quote with only the formatting I put into the document.write phrase.

I have tried using .innerHTML to send the document.write to another part of the page, but the same problem persists.

What can I do to make sure that the quote shows up on the page, where I want it to?

解决方案

There is no issue with document.write, it is doing exactly what it is supposed to do:

Overwrite the page with the new content.

If you do not want to do that, then you have to give it some context to write to.

For example:

function getQuote(){
    var textArea = document.getElementById('textArea');
    textArea.innerHTML = "Your Quote Is: $", price, ".00";
}

Which puts whatever your text is into a DOM element with id="textArea"

这篇关于Document.write问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆