Javascript If 语句运行一些 html 代码
我的 html 中嵌入了一些 JavaScript(使用 .aspx 文件)。我想执行某种 if 语句,然后确定是否显示某种图表。该图表使用 html 显示,我假设 if 语句应该用 javascript 编写。但是,我真的不知道如何从 java 中“运行”这个 html 代码。基本上就是画一张桌子。有什么建议吗?我见过 document.write,但我只看到它与单行一起使用。
I've got a little bit of javascript embedded in my html (using a .aspx file). I want to perform some sort of if statement which then determines whether or not some sort of chart is displayed. This chart is displayed using html, and I'm assuming the if statement should be written in javascript. However, I don't really know how to "run" this html code from within java. It is basically just drawing a table. Any suggestions? I've seen document.write, but I've only seen that being used with single lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您并没有真正“运行” HTML 代码。 HTML 是一种标记语言,主要用于格式化和排列 Web 浏览器中显示的元素。
您可能想要解决的问题是:根据某些条件显示或隐藏元素。像这样的 JavaScript 代码就是您想要的。
当然,负责显示图表的任何元素都应该具有
id="chart"
属性。例如。
我给出的 JavaScript 代码会更改此元素的
display
CSS 属性以隐藏它或使其可见。如果这不是您想要的,但您想要动态修改负责图表的 HTML,那么您需要使用元素的
innerHTML
属性。例子:
You don't really "run" an HTML code. HTML is a markup language and it is mostly used to format and arrange elements that are displayed in the web browser.
The problem you are probably trying to solve is: Display or hide an element based on some condition. A JavaScript code like this is what you want.
Of course whatever element is responsible for displaying the chart should have an
id="chart"
attribute. e.g.<div id="chart"><!-- Chart related code goes here --></div>
.The JavaScript code I have given alters the
display
CSS property of this element to hide it or make it visible.In case this is not what you want but you want to dynamically modify the HTML responsible for the chart, then you need to use the
innerHTML
property of the element.Example:
除非您正在测试只能在 JS 中找到的东西,然后在 ASP.NET 代码的服务器端执行它。
Opera 的 WSC 第 47 章对此进行了介绍: 创建和修改 HTML。您可能希望先阅读前面的一些章节。
Java 与 JavaScript 的共同点就像 Car 与 Carpet 一样多。它们是完全不同的编程语言。
Unless you are testing something that you can only find out out in JS, then do it server side in your ASP.NET code.
This is covered by chapter 47 of Opera's WSC: Creating and modifying HTML. You may wish to read some of the earlier chapters first.
Java has about as much in common with JavaScript as Car does with Carpet. They are completely different programming languages.
尝试在 javascript 中编写 if 语句,然后使用 JQuery html() 函数渲染 html。只需使用自定义 html id 来定位 html 代码所在的位置即可。
在这里阅读有关 html() 的更多信息: http://api.jquery.com/html/
YourHtmlString & ; DifferentHtmlString 是您应该以字符串格式存储自定义 html 的位置。然后它将在您包含“div id = 'custom-id'”的地方呈现,
希望这有帮助!
Try writing the if statement in javascript and then rendering the html with the JQuery html() function. Just use a custom html id to locate where you want the html code to go.
Read more about html() here: http://api.jquery.com/html/
YourHtmlString & DifferentHtmlString is where you should store your custom html in string format. It will then be rendered wherever you included "div id = 'custom-id'
Hope this helps!