开发者

Using jQuery to get img id and select node from XML data

开发者 https://www.devze.com 2023-02-05 01:00 出处:网络
I trying to create an app that helps demonstrate different tools in AutoCAD with an animated gif slideshow, a list of associated commands and a description. The information loads upon clicking an imag

I trying to create an app that helps demonstrate different tools in AutoCAD with an animated gif slideshow, a list of associated commands and a description. The information loads upon clicking an image icon, a snippet is provided below.

<img class="toolicons" id="rectangle" />
<img class="toolicons" id="circle" />

Which affects the following:

<p id="toolName">Tool Name</p>
<p id="toolCmd">Tool Command 1, Tool Command 2</p>
<p id="toolDesc">Tool Description</p>
<img id="toolSlide" src="default-placeholder.gif" 开发者_开发问答/>

I want the information to be retrieved from an XML file since there are several tools that I might describe later and add to the XML, also wanting to avoid defining long string variables for the description.

<ACAD>
  <TOOL>
    <NAME>rectangle</NAME>
    <CMD> 
      <CMD_01>REC</CMD_01>
      <CMD_02>RECTANG</CMD_02>
      <CMD_03>RECTANG</CMD_03>
    </CMD>
    <DESCRIPTION>Draws a rectangle</DESCRIPTION>
    <SLIDESHOW>rectangle.gif</SLIDESHOW>
  </TOOL>

  <TOOL>
    <NAME>circle</NAME>
    <CMD> 
      <CMD_01>C</CMD_01>
      <CMD_02>CIRCLE</CMD_02>
    </CMD>
    <DESCRIPTION>Draws a circle</DESCRIPTION>
    <SLIDESHOW>circle.gif</SLIDESHOW>
  </TOOL>
</ACAD>

I'm trying to use jQuery to do this. I shouldn't be using .each() since I only want to retrieve one entity, not parse all of them.

I've attempted to look this up but I can't find anything demonstrating a comparison between HTML id attribute and XML data, then taking just the data.

$(function(){
    $(".toolicons").click(function(){ 
      $.ajax({
        type: "GET",
        url: "acad.xml",
        dataType: "xml",
        success: function(xml) {
          $(xml).find('TOOL').each(function(){
            var tName = $(this).find('NAME').text();    
            var tDesc = $(this).find('DESCRIPTION').text();
            $(this).find('CMD').each(function(){
              var tCmd1 = $(this).find('CMD_01').text();
              var tCmd2 = $(this).find('CMD_02').text();
              var tCmd3 = $(this).find('CMD_03').text();
            });
          });
        }
        // change html in p elements from xml data
      });
    });
});


You can use :contains() selector

Code example (without error checking neither validation):

jQuery(document).ready(function()
{
    jQuery(".toolicons").click(function(e)
    {
        e.preventDefault();

        var id = jQuery(this).attr('id');

        jQuery.ajax({
            type: "GET",
            url: "acad.xml",
            dataType: "xml",
            success: function(xml) {
                var jqToolName = jQuery(xml).find('NAME:contains("' + id + '")');
                var jqTool = jqToolName.parent();
                var tName = jqTool.find('NAME').text();
                var tDesc = jqTool.find('DESCRIPTION').text();

                // console.log(tName, tDesc);
            }
        });
    });
});

Anyway, i will program it in a different way:

  • load the XML only once, on first use or after page/DOM load,
  • parse the XML data and store it in a javascript structure, with the tool name as key
  • when an image is clicked, gets its ID and look for the tool information in the javascript structure

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消