简体   繁体   中英

How to load local html in div from svg <a> click

I am trying to load a new local html page within a specific div when I click on an object within svg. Here is the code I have.

<html>

<head>
<script> 
$("#button").click(function(){
    $("#div1").load("1.html");
});

</script>


<svg width="100%" height="95%">
      <a xlink:href="#" id="button" class="button">
    <text x="150" y="45" fill="red">My Div</text>
  </a>

    </svg>

<style type="text/css"> 
#div1 {
    position:absolute;
    top: 15%;
    left: 15%;
    width:300px;
    height:300px;
    margin-top: -9px; /*set to a negative number 1/2 of your height*/
    margin-left: -15px; /*set to a negative number 1/2 of your width*/
    border: 1px solid #FFFFFF;
    background-color: none;
}

</style>


<div id="div1">Div Placement</div>



</body>
</html>

I've fiddled with this for a few days with no avail. I do not want to use the div hide/show method if at all possible.

Many thanks!

~ Leo

EDIT

okie I've tried doing what you did on the fiddle but it still will not change the div.

here is what I have now. Yes an index exists within the pages folder.

<html>


 <head>

 <script type="text/javascript" src="/js.jquery/jquery-2.1.1.min.js">         </script>    
 <script> 
$("#button").click(function(){
$("#div1").text("Loading...");
$("#div1").load("/pages/");
});</script>    



 </head>

  <body bgcolor="#000000">

  <svg width="100%" height="95%">

   <a xlink:href="#" id="button" class="button">
   <text x="150" y="45" fill="red">My Div</text>
 </a>


   </svg>


     <div id="div1">Div Placement</div>

EDIT

Third iteration of code, still not working

<html>


<head>
  <meta charset="UTF-8">

  <title>Float Test</title>

    <link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>   
<script> 
    $("#button").click(function(){
    $("#div1").text("Loading...");
    $("#div1").load("/pages/");
});</script>    



</head>

<body bgcolor="#000000">

<svg width="100%" height="95%">


        <a xlink:href="#" id="button" class="button">
    <text x="150" y="45" fill="red">My Div</text>
  </a>


    </svg>




<div id="div1">Div Placement</div>



</body>
</html>

It works fine for me.

http://jsfiddle.net/sumzujre/

The main problem seems to be that you aren't including the jQuery library. Add this line to your page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Also, make sure that "1.html" exists. If the server returns an error (eg. 404), nothing will be inserted into your div.

Update:

You will need to make sure the elements exist before you try to add event handlers to them. Either move your <script> block to the end of the file, or you can leave it at the top and surround it with $.ready() .

<script> 

$(document).ready(function() {

   $("#button").click(function(){
      $("#div1").text("Loading...");
      $("#div1").load("/pages/");
   });

});

</script>    

I always use ajax . Like this

<div id="my-content"></div> 
<a id="button">Click to load</a> 
<script type="text/javascript">
 $('#button').live('click',function(){
 $.ajax({ 
   method: 'POST', 
   url:'yourPage.html', 
   success:function(data){
      $('#my-content').html(data)
   } 
})}) 
</script>
<html>


<head>

  <title>Float Test</title>


</head>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>   





<body bgcolor="#FFFFFF">




<svg width="100%" height="95%">


        <a xlink:href="#" id="button" class="button">
    <text x="150" y="45" fill="red">My Div</text>
  </a>


    </svg>



<style type="text/css"> 
#div1 {
    position:absolute;
    top: 15%;
    left: 15%;
    width:300px;
    height:300px;
    margin-top: -9px; /*set to a negative number 1/2 of your height*/
    margin-left: -15px; /*set to a negative number 1/2 of your width*/
    border: 1px solid #FFFFFF;
    background-color: none;
}

</style>


<div id="div1">Text</div>

Working iteration of code. I had removed the hashtag within

<a xlink:href="#" id="button" class="button">

in my working code. Thank you for your patience with my lack of thoroughness

</body>
</html>
<script> 
$("#button").click(function(){
    $("#div1").load("1.html");
});

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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