简体   繁体   English

动态地将对象值添加到div里面 <p> 标签

[英]Dynamically add object value to div inside <p> tag

On click of a button, I want to add ap tag to an existing div with the value of an object property name inside of it. 单击一个按钮,我想将ap标签添加到现有div中,其中包含对象属性名称的值。

  var foodLoads = {
    orange: 6.7,
    apple: 5.6,
    banana: 12.7,
    grapes: 16.3,
    peach: 2.7,
    pear: 6.5,
    mango: 16.1,
    blueberries: 9.3,
    grapefruit: 1.7,
    strawberry: 3.5,
    tangerine: 3.1,
    watermelon: 8,
    duck: 0,
    beef: 0,
    chicken: 0,
    ham: 0,
    turkey: 0,
    elk: 0,
    pork: 0,
    fish: 0,
    eggs: 0,
    lamb: 0,
    applejuice: 11.8,
    cranberryjuice: 23.3,
    orangejuice: 14.4,
    carrotjuice: 8.6,
    lemonade: 24.3,
    hotchocolatemix: 10.2,
    tomatojuice: 3.5,
    chocolatemilk: 13.3,
    almondmilk: 0.02,
    soymilk: 4,
    wholewheatbread: 6.1,
    whitebread: 10.7,
    bagel: 30,
    waffle: 13.8,
    pancake: 5.3,
    croissant: 12.2,
    muffin: 28.8,
    englishmuffin: 21.3,
    doughnut: 15.2,
    oatmeal: 12.6,
    quinoa: 20.4,
    wholegrainbread: 7.1 
  }

The divs are created dynamically when a user checks a checkbox (this is before they click the button). 当用户选中复选框时(这是在他们单击按钮之前)动态创建div。

  $('input').on('ifChecked', function(event) {       
    var liText = $(this).parent().parent().text();
    var wrappedUp = $('<div class="active"><li>' + liText + '</li><select class="serving-size"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></div>');
    $('.food-list').append(wrappedUp);
    $(wrappedUp).addClass('' + liText + '');        
})

This is an example of the divs I'm talking about 这是我正在谈论的div的一个例子

<ul class="food-list">

    <div class="active Mango">
                 <li>Mango</li>
          <select class="serving-size"><option value="1">1</option><optionvalue="2">2</option><option value="3">3</option><option value="4">4</option>
          </select>
    </div>
</ul>

So, basically, on click of a button I want to be able to use the class (ie Mango) to match it with the property in the foodLoads object with the same name (mango), put that property's value in ap tag, and add it to the div with the matching class (Mango). 因此,基本上,单击一个按钮,我希望能够使用该类(即Mango)将其与foodLoads对象中具有相同名称(mango)的属性匹配,将该属性的值放在ap标记中,然后添加它与匹配类(芒果)的div。

This is the jQuery I was trying to use. 这是我试图使用的jQuery。 As I am new to jQuery I'm sure the syntax is all screwed up. 因为我是jQuery的新手,所以我确信语法都搞砸了。

  $('.submit-items').click(function() {
    $.each( foodLoads, function( key, value ) {
      if ($('.active:contains(' + key + ')')) {
        $(this).append('<p>' + key + '</li>');
      }
})

     })

Any ideas? 有任何想法吗? Thanks in advance :) 提前致谢 :)

Here is a simpler approach. 这是一种更简单的方法。

  • find the text of the li from the active class. active类中查找li的文本。
  • look up its value. 抬头看它的价值。
  • append the p 附加p

like:- 喜欢:-

$('.submit-items').click(function() {
   var type = $('.food-list .active li').text();
   var value = foodLoads[type.toLowerCase()];
   $('.' + type).append('<p>' + value + '</p>');
});

Demo 演示

 var foodLoads = { orange: 6.7, apple: 5.6, banana: 12.7, grapes: 16.3, peach: 2.7, pear: 6.5, mango: 16.1, blueberries: 9.3, grapefruit: 1.7, strawberry: 3.5, tangerine: 3.1, watermelon: 8, duck: 0, beef: 0, chicken: 0, ham: 0, turkey: 0, elk: 0, pork: 0, fish: 0, eggs: 0, lamb: 0, applejuice: 11.8, cranberryjuice: 23.3, orangejuice: 14.4, carrotjuice: 8.6, lemonade: 24.3, hotchocolatemix: 10.2, tomatojuice: 3.5, chocolatemilk: 13.3, almondmilk: 0.02, soymilk: 4, wholewheatbread: 6.1, whitebread: 10.7, bagel: 30, waffle: 13.8, pancake: 5.3, croissant: 12.2, muffin: 28.8, englishmuffin: 21.3, doughnut: 15.2, oatmeal: 12.6, quinoa: 20.4, wholegrainbread: 7.1 }; $('.submit-items').click(function() { var type = $('.food-list .active li').text(); var value = foodLoads[type.toLowerCase()]; $('.' + type).append('<p>' + value + '</p>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="food-list"> <div class="active Mango"> <li>Mango</li> <select class="serving-size"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </div> </ul> <button class="submit-items">submit</button> 

如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag?

暂无
暂无

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

相关问题 无法将动态创建的“ p”标签和“ img”标签包装在动态创建的“ div”标签内 - unable to wrap dynamically created “p” tag and “img” tag inside a dynamically created “div” tag 玉模板引擎无法添加 <p> 标记到div的内部 - Jade template engine could not add <p> tag to the inside of div 创建一个 <div> 在里面 <p> 动态 - Creating a <div> inside a <p> dynamically 如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag? div 或 p 标记在 flex box 包装内 - div or p tag inside of flex box wrapping 如何添加多个元素,例如<span>,</span> <p> <span>要么</span> <div> <span>内</span> <option> <span>的标签</span> <select> <span>在ReactJs中?</span> - How can I add multiple elements like <span>, <p> or <div> inside <option> tag of <select> in ReactJs? 在 <div> 并将文字放在标签内 <p></ p> - Search a word within a <div> and put text inside a tag <p> </ p> 从div中的ap标签中选择一个值 - Selecting a value from a p tag within a div 将div封闭在带有data-role =“ page”的div中后,无法动态添加锚标记 - Can't dynamically add anchor tag after enclosing div inside a div with data-role=“page” 在文本标签内添加div - Add a div inside the text tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM