Javascript If 语句运行一些 html 代码

发布于 2024-11-15 00:37:40 字数 198 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十秒萌定你 2024-11-22 00:37:40

您并没有真正“运行” HTML 代码。 HTML 是一种标记语言,主要用于格式化和排列 Web 浏览器中显示的元素。

您可能想要解决的问题是:根据某些条件显示或隐藏元素。像这样的 JavaScript 代码就是您想要的。

if (condition) {
    document.getElementById('chart').style.display = "none"
} else {
    document.getElementById('chart').style.display = ""
}

当然,负责显示图表的任何元素都应该具有 id="chart" 属性。例如

我给出的 JavaScript 代码会更改此元素的 display CSS 属性以隐藏它或使其可见。

如果这不是您想要的,但您想要动态修改负责图表的 HTML,那么您需要使用元素的 innerHTML 属性。

例子:

if (condition) {
    document.getElementById('chart').innerHTML = "<!-- HTML Code for the chart here -->"
} else {
        document.getElementById('chart').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.

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>.

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:

if (condition) {
    document.getElementById('chart').innerHTML = "<!-- HTML Code for the chart here -->"
} else {
        document.getElementById('chart').innerHTML = ""
}
负佳期 2024-11-22 00:37:40

我假设 if 语句应该用 JavaScript 编写

除非您正在测试只能在 JS 中找到的东西,然后在 ASP.NET 代码的服务器端执行它。

我真的不知道如何从内部“运行”这个 html 代码

Opera 的 WSC 第 47 章对此进行了介绍: 创建和修改 HTML。您可能希望先阅读前面的一些章节。

java

Java 与 JavaScript 的共同点就像 Car 与 Carpet 一样多。它们是完全不同的编程语言。

I'm assuming the if statement should be written in 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.

I don't really know how to "run" this html code from within

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

Java has about as much in common with JavaScript as Car does with Carpet. They are completely different programming languages.

是你 2024-11-22 00:37:40

尝试在 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>

在这里阅读有关 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.

<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/

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文