简体   繁体   中英

Add element to a div with jquery or javascript

The problem is : i need to add text into div but i cant. We have two views , id="list view " and id="grid view" . i am trying to add a notification bar with this js code and i can add what i want in to list view but cant add in to grid view. .

this is "LİST" html code ,

<div id="list-view" style="">
     <div class="package-details-list">
           <div class="package-details-p1">
           <div class="package-details-p2">
           <div class="package-details-p3">
     <div class="package-details-list">
     <div class="package-details-list">
     <div class="package-details-list">
     <div class="package-details-list">
     <div class="package-details-list">
     <div class="package-details-list">
<div class="package-details-list">

this is "GRİD" html code

<div id="list-view" style ="" >
<div id="grid-view" style="none">
    <div class="package-grid-row clearfix">
           <div class="package-details-grid">
                  <a class="package-picture" href="http.abc.com">
                  <h2>
                  <p>
           <div class="package-details-grid">
           <div class="package-details-grid">
           <div class="package-details-grid">
           </div>
    <div class="package-grid-row clearfix">
    <div class="package-grid-row clearfix">
    <div class="package-grid-row clearfix">
</div>

i am trying to add text into "#package-details-grid" but i need to do this randomly.

this is my js code ,

 (function($){    
      var gridp = $("<div></div>");    
      gridp.css({height:'17px',    
      paddingRight: '5px',    
      paddingLeft: '5px',     
      textAlign: 'center',    
      // letterspacing: '2px',   
      lineHeight: '14px',     
      //overflow: 'hidden',    
      fontSize: '12px',     
      fontWeight: 'bold',     
      color: 'white',     
      backgroundColor: '#0E71B8',     
      fontFamily:'GothamNarrowMedium',    
      position: 'relative',   
      width: '100%',   
      // right: '412px' ,   
      // margin: '-56px 0px 0px 760px'   

      })

      var div = $('<div></div>');
      div.css({
      height: '30px',    
      paddingRight: '20px',    
      paddingLeft: '20px', 
      textAlign: 'center',    
      // letterspacing: '2px',    
      lineHeight: '28px',     
      overflow: 'hidden',    
      fontSize: '12px',     
      fontWeight: 'bold',     
      color: 'white',     
      backgroundColor: '#0E71B8',     
      fontFamily:'GothamNarrowMedium',    
      position: 'absolute',  
      //  width: '100%',    
      right: '412px' ,    
      margin: '-56px 0px 0px 760px',    
      hover :( 'color : #00A0DE')  

      })   

      var hcseen = [];    
      var gridseen = [];
        for (k=0; k<=2; k++ )
        {    
            griddiv = Math.floor((Math.random() * 3 ) + 1 );   
            gridcount = Math.floor((Math.random() * 3 ) + 1 );    
            hcount = Math.floor((Math.random() * 10) + 1);    
            if (!hcseen["s"+hcount] || !griddiv["s"+griddiv]) 
            {        
                 hcseen["s"+hcount] = 1;        
                 gridseen["s"+griddiv]=1;         
                 count = Math.floor((Math.random() * 3) + 1);                
                 div.clone().text("xyz "+ count + " add").insertAfter('#list-view .package-details-list:eq('+ hcount +')');                
                 gridp.clone().text("abc "+ count + " add").insertAfter('#grid-view .package-grid-row clearfix');         
    }                 
                else 
    {       
                k--;    
    }
    }
    }) (jQuery);

it works on list view, i tried whatever i can but it doesnt work on grid view. Thanks.

I am not sure if I got exactly what you are trying to accomplish but this is now has the text appearing. I also moved the CSS changes to classes to simplify the JS.

https://jsfiddle.net/qm1fwq85/1/

$().ready(function () {
var gridp = $("#grid-view");
$(gridp).addClass("gridStyle");

var divs = $("#list-view").find('div');
for (var i; i < divs.length; i++) {
    $(divs[i]).addClass("divStyle");
}

var hcseen = [];
var gridseen = [];
for (k = 0; k <= 2; k++) {
    griddiv = Math.floor((Math.random() * 3) + 1);
    gridcount = Math.floor((Math.random() * 3) + 1);
    hcount = Math.floor((Math.random() * 10) + 1);
    if (!hcseen["s" + hcount] || !griddiv["s" + griddiv]) {
        hcseen["s" + hcount] = 1;
        gridseen["s" + griddiv] = 1;
        count = Math.floor((Math.random() * 3) + 1);
        $(divs[0]).text("xyz " + count + " add").insertAfter('#list-view .package-details-list:eq(' + hcount + ')');
        $(gridp).text("abc " + count + " add").insertAfter('#grid-view .package-grid-row clearfix');
    } else {
        k--;
    }
}
});

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