Javascript If 语句运行一些 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6308570/
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
Javascript If statement to run some html code
提问by Hani Honey
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.
我的 html 中嵌入了一些 javascript(使用 .aspx 文件)。我想执行某种 if 语句,然后确定是否显示某种图表。该图表使用 html 显示,我假设 if 语句应该用 javascript 编写。但是,我真的不知道如何从 java 中“运行”这个 html 代码。它基本上只是画一张桌子。有什么建议?我见过 document.write,但我只见过它与单行一起使用。
采纳答案by Susam Pal
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.
您并没有真正“运行”HTML 代码。HTML 是一种标记语言,主要用于格式化和排列 Web 浏览器中显示的元素。
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.
您可能要解决的问题是:根据某些条件显示或隐藏元素。像这样的 JavaScript 代码正是您想要的。
if (condition) {
document.getElementById('chart').style.display = "none"
} else {
document.getElementById('chart').style.display = ""
}
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>
.
当然,负责显示图表的任何元素都应该有一个id="chart"
属性。例如<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.
我给出的 JavaScript 代码改变了display
这个元素的CSS 属性来隐藏它或使它可见。
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.
如果这不是你想要的,但你想动态修改负责图表的 HTML,那么你需要使用innerHTML
元素的属性。
Example:
例子:
if (condition) {
document.getElementById('chart').innerHTML = "<!-- HTML Code for the chart here -->"
} else {
document.getElementById('chart').innerHTML = ""
}
回答by Quentin
I'm assuming the if statement should be written in javascript
我假设 if 语句应该用 javascript 编写
Unless you are testing something that you can only find out out in JS, then do it server side in your ASP.NET code.
除非你正在测试一些你只能在 JS 中找到的东西,否则在你的 ASP.NET 代码中做服务器端。
I don't really know how to "run" this html code from within
我真的不知道如何从内部“运行”这个 html 代码
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.
Opera 的 WSC:创建和修改 HTML的第 47 章涵盖了这一点。您可能希望先阅读前面的一些章节。
java
爪哇
Java has about as much in common with JavaScript as Car does with Carpet. They are completely different programming languages.
Java 与 JavaScript 的共同点几乎与 Car 与 Carpet 的共同点一样多。它们是完全不同的编程语言。
回答by Gideon Rosenthal
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.
尝试在 javascript 中编写 if 语句,然后使用 JQuery html() 函数呈现 html。只需使用自定义 html id 来定位您想要 html 代码的位置。
<div id = "custom-tag"> </div>
<script>
if (true){
$('#custom-tag').html('YourHtmlString');
} else {
$('#custom-tag').html('DifferentHtmlString');
}
</script>
Read more about html() here: http://api.jquery.com/html/
在此处阅读有关 html() 的更多信息: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'
YourHtmlString 和 DifferentHtmlString 是您应该以字符串格式存储自定义 html 的位置。然后它将在您包含“div id = 'custom-id'的任何地方呈现
Hope this helps!
希望这可以帮助!