如何使用JavaScript将XML数据显示到TML灯箱?

时间:2018-09-11 06:43:38

标签: javascript html xml

我试图在弹出框中显示XML数据。现在,它在弹出窗口中显示XML文件数据的所有数据。但是实际上,当我单击open modal1时,只需要从XML文件中加载第一个数据集(array-0)。另外,当单击打开modal2时,只需要从XML加载第二个数据集(array-1),如何使用js循环来完成呢?

这是我的代码。

data.xml

<?xml version="1.0"?>
<paintings>
  <painting>
    <title>test</title>
    <artist>Test Artist</artist>
    <image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
    <price>850</price>
  </painting>

  <painting>
    <title>test2</title>
    <artist>Test Artist2</artist>
    <image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
    <price>8503</price>
  </painting>
</paintings>

index.html

<div class="container">


<a href="#openModal1" id="openModalBtn">open modal1</a>
<a href="#openModal2" id="openModalBtn">open modal2</a>


<div id="openModal1" class="modalDialog">
    <div>
       <input class="getAssignment2" type="button" value="Previous" id="prev">
        <input class="getAssignment" type="button" value="Next" id="next">
    <a href="#close" title="Close" class="close">X</a>
   <div id="container"></div>
   </div>
</div>

</div>

JavaScript代码

$(document).ready(function(){
        $.ajax({
            type:"GET",
            url:"data.xml",
            dataType:"xml",
            success:xmlParser
        });
    });

    function xmlParser(xml){
        $(xml).find("painting").each(function(){
            $("#container").append(
                '<div class="painting"><img src="' + $(this).find("image").text() + '" width="200" height="225" alt="' + $(this).find("title").text() + '" /> <br/> <div class="title">' + $(this).find("title").text() + '<br/>$' + $(this).find("price").text() + '</div></div>');

        });
    }

1 个答案:

答案 0 :(得分:0)

以下代码可以为您提供帮助

$('.modal-click').click(function(){
		xmlParser(xml,this.id);
	})
	var xml = '<paintings><painting><title>test</title><artist>Test Artist</artist><image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image><price>850</price></painting><painting><title>test2</title><artist>Test Artist2</artist><image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image><price>8503</price></painting></paintings>'
	    function xmlParser(xml,id){
	        	console.log($(this).find("title").text())
	            $("#container-"+parseInt(id)).append(
	                '<div class="painting"><img src="' + $($(xml).find("image")[parseInt(id)]).text() + '" width="200" height="225" alt="' + $($(xml).find("title")[parseInt(id)]).text() + '" /> <br/> <div class="title">' + $($(xml).find("title")[parseInt(id)]).text() + '<br/>$' + $($(xml).find("price")[parseInt(id)]).text() + '</div></div>');

	    }
	    $('#myModal, #myModal1').on('hidden.bs.modal', function () {
			$('.text-container').empty();
		})
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="modal-click btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" id="0">Open Modal 1</button>

  <button type="button" class="modal-click btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1" id="1">Open Modal 2</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header 1</h4>
        </div>
        <div class="modal-body">
          <div class="container text-container" id="container-0"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  <div class="modal fade" id="myModal1" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header 2</h4>
        </div>
        <div class="modal-body">
          <div class="container text-container" id="container-1"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>