简体   繁体   English

如何在html内的javascript中显示某些文本?

[英]How can I display certain text from a javascript inside the html?

I'm just starting to learn javascript, so I don't know how everything works. 我才刚刚开始学习JavaScript,所以我不知道一切如何运作。 I have this function: 我有这个功能:

<div id="box1">
  <a id="link1">Link</a>
  <div><script>
   function script() {

var producto,cantidad,precio,tp,imagen;

producto=prompt("Ingresar producto:");
cantidad=parseInt(prompt("Ingresar cantidad:"));

switch (producto) {
  case "disco":
    precio=200;imagen="disco.jpg";break;
  case "micro":
    precio=800;imagen="micro.jpg";break;
  case "placa":
    precio=500;imagen="placa.jpg";break;
  case "memoria":
    precio=800;imagen="memoria.jpg";break;
  case "case":
    precio=100;imagen="case.jpg";break;
  case "tarjeta":
    precio=300;imagen="tarjeta.jpg";break;
  default:
    alert("No se encuentra el producto en lista");
}
tp=precio*cantidad;

document.write( "Producto: "+producto+"<br>");
document.write("Precio: "+precio+"<br>");
document.write("Cantidad: "+cantidad+"<br>");
document.write("<br>");
document.write("Total a pagar: "+tp+"<br>");
document.write("<img src="+imagen+">");
};
document.getElementById('link1').onclick = function () {
script();
};
</script></div>

When I click the "link1" the result appears in a blank page, how can I display the info inside the same page? 当我单击“ link1”时,结果将显示在空白页面中,如何在同一页面内显示信息?

I suggest you create a div where you want to display the results, for example: 建议您在要显示结果的位置创建一个div,例如:

<div id = "blablabla"></div><br>

Then use 然后使用

document.getElementById('blablabla').innerHTML="Producto: "+producto+"<br>";

in your JavaScript to display the results in the div. 在您的JavaScript中将结果显示在div中。

As far what I understood is that "You're trying to display information on a same page using javascript"? 据我了解,“您正在尝试使用JavaScript在同一页面上显示信息”? If you're trying something similar to this then continue reading... 如果您正在尝试类似的操作,请继续阅读...
How Is This Possible? 这怎么可能?
This is possible if you create a <div> tag on same page and assign an id to it & then use javascript DOM to show text in it. 如果您在同一页面上创建一个<div>标记并为其分配一个ID,然后使用javascript DOM在其中显示文本,则可以这样做。 Let's start doing it. 让我们开始吧。 Step 1:- 第1步:-

<div id="TextGoesHere"></div>
<a id="link1">Link</a>
<script>
   function script() {

var producto,cantidad,precio,tp,imagen;

producto=prompt("Ingresar producto:");
cantidad=parseInt(prompt("Ingresar cantidad:"));

switch (producto) {
  case "disco":
    precio=200;imagen="disco.jpg";break;
  case "micro":
    precio=800;imagen="micro.jpg";break;
  case "placa":
    precio=500;imagen="placa.jpg";break;
  case "memoria":
    precio=800;imagen="memoria.jpg";break;
  case "case":
    precio=100;imagen="case.jpg";break;
  case "tarjeta":
    precio=300;imagen="tarjeta.jpg";break;
  default:
    alert("No se encuentra el producto en lista");
}
tp=precio*cantidad;
var div = document.getElementById("TextGoesHere");
div.innerHTML = div.innerHTML + "Producto: "+producto+"<br>";
div.innerHTML = div.innerHTML + Precio: "+precio+"<br>";
// Similarly you just have to copy above line paste again and replace variables. 
};
document.getElementById('link1').onclick = function () {
script();
};
</script>

What .innerHTML does? .innerHTML是什么? If you are familiar with other languages such as java or etc there is function of append() which concatenates new element with previous one. 如果您熟悉其他语言,例如Java或其他语言,则可以使用append()函数将新元素与上一个元素连接在一起。 Similarly .innerHTML can be used to append new element or text into tag preceding with DOM. 同样,.innerHTML可用于将新元素或文本附加到DOM之前的标记中。
Tip: try to use type="text/javascript" in <script> tag because when you validate your code with w3c validator your code won't be validated because of this :) 提示:尝试在<script> tag使用type="text/javascript" ,因为当您使用w3c验证程序验证代码时,由于以下原因,您的代码将无法验证:)

Replace this : 替换为:

document.write( "Producto: "+producto+"<br>");

With

document.getElementById('link1').innerHTML="Producto: "+producto+"<br>";

暂无
暂无

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

相关问题 如何在 Javascript 中的另一个 index.html 网页中以文本格式而不是 URL 显示对象属性? - How can I display the object property in text format inside the another index.html webpage in Javascript instead of an URL? 如何在 html 中显示来自 Javascript 数组的随机引用? - How can I display an random quote from an Javascript Array in html? 如何用多行文本显示从JavaScript到HTML的文本 - How to display text with multiline from JavaScript to the HTML 如何使用JavaScript在HTML中显示文本文件中的文本 - How to display text from a textfile in html with javascript CSS,JavaScript,HTML,如何将div中的文本与图像旁边的中间对齐? 并为文本设置动画 - CSS, JavaScript, HTML, how can i align a text inside a div to middle beside an image? and animate the text 如何替换javascript中javascript中的某些字母? - How to I replace certain letters from javascript inside javascript? HTML/Javascript - 如何将输入字段中的文本显示为 HTML 代码? - HTML/Javascript - How to display text from input field as HTML code? 如何从DIV中的选定文本中剥离某些标签? - How can I strip certain tags from selected text inside a DIV? 如何在javascript中显示表的某些列的总和? - How can I display the sum of certain columns of a table in javascript? 如何在iframe中将页面内容获取为JavaScript中的字符串? 我想要一切 <html> 至 </html> - How can I get page content inside an iframe as string in javascript? I want everything from <html> to </html>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM