简体   繁体   English

使用jQuery或JavaScript将元素添加到div

[英]Add element to a div with jquery or javascript

The problem is : i need to add text into div but i cant. 问题是:我需要将文本添加到div中,但我不能。 We have two views , id="list view " and id="grid view" . 我们有两个视图, id =“ list viewid =“ 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. 我正在尝试使用此js代码添加一个通知栏,我可以将想要添加的内容添加到列表视图中,但不能添加到网格视图中。 .

this is "LİST" html code , 这是“LİST” html代码,

<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 这是“GRİD” html代码

<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. 我正在尝试将文本添加到“#package-details-grid”中,但是我需要随机执行此操作。

this is my js code , 这是我的js代码,

 (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. 我还将CSS更改移到了类上以简化JS。

https://jsfiddle.net/qm1fwq85/1/ 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--;
    }
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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