简体   繁体   English

如何通过使用jQuery和XML数据更改使用CSS的HTML元素?

[英]How to change an HTML element that uses CSS by using jQuery and XML data?

I have a site that shows system status that has a colored bar behind it depending on the health of the system it displays. 我有一个显示系统状态的站点,该站点后面有一个彩色条,具体取决于显示的系统的运行状况。 My HTML that uses the jQuery is: 我使用jQuery的HTML是:

<script> 
  function myExampleSite() 
  {
    var myURL = window.location.href;
    var dashIndex = myURL.lastIndexOf("-");
    var dotIndex = myURL.lastIndexOf(".");
    var result = myURL.substring(dashIndex + 1, dotIndex);
    return result;
  }

  var exampleSite = myExampleSite();
</script>

<script> 
var root = null;
$(document).ready(function ()
{
    $.get("Status_Pages.xml",
    function (xml)
    {
        root = $(xml).find("site[name='" + exampleSite + "']");

        var status = $(root).find("systemStatus");
        var color = status.attr("color");
        $("#status").html(status.text());
        $("#status").attr("color", color);

        result = $(root).find("networkNotes");
        $("#networkNotes").html($(result).text());

        result = $(root).find("headerImage");
        $("td#headerImage").html($(result).text());

       ....etc etc
    });
});
</script>

My XML file looks like this. 我的XML文件如下所示。

<sites>
   <site name="Template"> 
       <headerImage>images/template-header.png</headerImage>
       <productVersion>[Version goes here]</productVersion>
       <systemStatus color="green">Normal</systemStatus>
       <networkNotes>System status is normal</networkNotes>
    </site>
</sites>

I have several <site> s that all have their own data that will populate different areas of individual sites. 我有几个<site> ,它们都有自己的数据,这些数据将填充各个站点的不同区域。 I've ran into a snag though. 不过我遇到了麻烦。

The way I am displaying my status currently looks like this: 我目前显示状态的方式如下所示:

<tr>
    <td class="table_header">
        Current System Status
    </td>
</tr>
    <table><tr>

        <td id="status"></td>

    </tr></table>

    <tr>
        <td class="table_header">
        Network Notes
    </td>
    </tr>

Right now it's hard-coded to show the system status with either a red, green, or yellow bar behind the text to indicate the health of the system, but I need to make able to read the XML value for that site. 现在,它已经过硬编码以显示系统状态,并在文本后带有红色,绿色或黄色条形文字,以指示系统的运行状况,但是我需要能够读取该站点的XML值。

The tricky part is that I am also using data from a CSS for this particular portion of the site. 棘手的部分是,我还在网站的此特定部分使用来自CSS的数据。 The CSS code relevant to this is (there is one for green, yellow, and red): 与此相关的CSS代码是(绿色,黄色和红色一个):

#status[color=green] {
    color: white;
    text-align: left;
    background: url(images/greenbar.jpg) no-repeat;
}

#status[color=yellow] {
    color: black;
    text-align: left;
    background: url(images/yellow.jpg) no-repeat;
}

#status[color=red]{
    color: black;
    text-align: left;
    background: url(images/red.jpg) no-repeat;
}

**How can I read in the system status string, along with the element's attribute of color to populate my HTML so that each site has a different image depending on what's in the XML? **如何读取系统状态字符串以及color的元素属性来填充HTML,以便每个站点根据XML中的内容具有不同的图像? ** **

I have tried the above and it gives a color background to the text, not the image url background. 我已经尝试了上面,它给文本提供了彩色背景,而不是图像URL背景。

And it reads the correct status but the image never appears behind it. 并且它读取正确的状态,但是图像从不出现在其后面。

If I understand your question, you have information in your XML that you want to show in your HTML. 如果我理解您的问题,那么您的XML中就有要显示在HTML中的信息。 And depending on the status/content of your XML, you also want it to be shown/presented/styled differently using CSS. 并且,根据XML的状态/内容,您还希望使用CSS以不同的方式显示/呈现/设置样式。

With CSS, you can select things, using ID, class or in the case of [color=green] , attributes. 使用CSS,您可以使用ID,类或[color=green]属性来选择事物。 But the code that you have supplied, you haven't attached the color attribute to your HTML. 但是,您提供的代码尚未将color属性附加到HTML。 So to make this work, with the CSS that you have, you need to do something like 因此,要使用现有的CSS进行此项工作,您需要执行以下操作

var status = $(root).find("systemStatus"); 
var color = status.attr("color"); //get the attribute value from the XML
$("#status").attr("color", color); //assign the attribute value to the HTML
$("#status").html(status.text());
result = $(root).find("networkNotes");
$("#networkNotes").html($(result).text());

An example of this 一个例子

But moving forward, semantically, I think you don't need to use color="green" . 但是,从语义上讲,我认为您不需要使用color="green" It actually not be the "correct" way (in terms of content and presentation seperation). 实际上,这不是“正确”的方式(就内容和表示分离而言)。 I would actually suggest using classes and assign them based on the actual "status". 我实际上建议使用类,并根据实际的“状态”分配它们。 So example normal , warning and danger . 因此,举例说明normalwarningdanger Then you can simply have the color information (or "presentation" information) be in the CSS file. 然后,您可以简单地将颜色信息(或“表示”信息)保存在CSS文件中。

So your XML for status will be something like 因此,您的XML状态信息将类似于

<systemStatus>Normal</systemStatus>

and the jQuery code would be something like 而jQuery代码将类似于

var status = $(root).find("systemStatus").text();
$("#status").html(status);
$("#status").attr("class", status.toLowerCase());

and your css will be something like 而你的CSS将是这样的

#status.normal {
    background:green
}
#status.congested {
    background:yellow;
    color:black
}

#status.down {
    background:red
}

Example

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM