简体   繁体   中英

Why does this div drop down when InnterHtml is changed?

I have a div of thumbs and was wanting to change the innerHtml on click; however, whenever I insert something into them, they drop down and I am wanting them to remain in line.

Here is the jsfiddle:

http://jsfiddle.net/jtDzs/

For example, if I wanted to add "something" to the boogie div in:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
      <title>TITLE</title>
      <style>
         .thumbContainer {
            width: 200px;
         }
         .thumb {
            width: 95px;
            height: 95px;
            background-color: blue;
            display: inline-block;
         }
      </style>
   </head>
      <div>
         <div class="thumbContainer">
            <div id="boogie" class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
      </div>
      <script>
         (function() {
            $(".thumb").bind("click", function() {
               $("#boogie").html("something");
            });
         })();
      </script>
   </body>
</html>

For some reason using display: inline-block is causing this problem...

There are two simple solutions:

Overflow: hidden

You can add this line to your class:

overflow: hidden;

Here's the working jsFiddle Demo


Float: left

You can also start using floating to the left instead of display: inline-block and add the wanted margin.

// display: inline-block
float: left
margin: 2.5px;

Here's the working jsFiddle Demo

PS I guessed you're trying to make this kind of behaviour to every div so I allowed myself to change the html changing command to:

$(this).html("something");

You can change this if you want

     .thumb {
        color: white;
        width: 95px;
        height: 95px;
        margin: 2.5px;
        background-color: blue;
        float: left;
     }

http://jsfiddle.net/jtDzs/9/

         (function() {
            $(".thumb").on("click", function() {
             $(this).("something");
           });
          })();

CSS is

     .thumb {
     color: white;
        width: 95px;
        height: 95px;
        background-color: blue;
        display: inline-block;
         float: left;
         margin: 2px;
     }

Demo

Hope it helps

 $(".thumb").on("click", function() {
               $(this).append("something");
            });

JSFIDDLE DEMO

 (function() {
        $(".thumb").html("&nbsp;");
        $(".thumb").bind("click", function() {
           $("#boogie").html("something");
        });
     })();


.thumbContainer {
        width: 200px;
        height:100px;
     }

http://jsfiddle.net/jtDzs/11/

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