简体   繁体   English

将 json 数据加载到展开的按钮中 - bootstrap 4 beta

[英]loading json data into button that expands - bootstrap 4 beta

Hi I'm trying to format and plug in some data from a js document, formatted as javascript objects, into a div within a collapsed div , which expands when you click a button.嗨,我正在尝试将js文档中的一些数据(格式化为 javascript 对象)格式化并插入到折叠div ,当您单击按钮时,该div会展开。 I'm using leaflet and working with the exact same data, and I've had no problem plugging the json data into map popups.我正在使用传单并使用完全相同的数据,并且将 json 数据插入地图弹出窗口没有问题。 jquery append isn't working at all, even if I just try to append text nothing shows up. jquery append根本不起作用,即使我只是尝试附加文本也没有显示任何内容。 I've tried about 10 different solutions from a bunch of different questions, but still nothing will show up.我从一堆不同的问题中尝试了大约 10 种不同的解决方案,但仍然没有任何结果。 I am using bootstrap 4 beta.我正在使用引导程序 4 测试版。

latest js attempt:最新的js尝试:

$(document).ready(function(){
    $('button').click(function(){

        $('#everydayFoodContent').append('<p>hello!</p>');
    });    
});

I've also tried just having我也试过只拥有

$('#everydayFoodContent').append('<p>hello!</p>');

on its own but that still doesn't work.靠它自己,但这仍然不起作用。 I've also tried this:我也试过这个:

$everydayFoodContent=$('#everydayFoodContent');
$everydayFoodContent.append('<p>hello!</p>');

I've also tried using " console.log('#everydayFoodContent'); " as I've seen other answers to similar questions suggest this to check and make sure the div is available(I think) but honestly I don't know what the purpose of console.log is or what I'm supposed to see if it does or doesn't work, and haven't been able to find anything online that concisely explains this.我也试过使用“ console.log('#everydayFoodContent'); ”,因为我已经看到类似问题的其他答案建议这样做检查并确保 div 可用(我认为)但老实说我不知道console.log的目的是什么,或者我应该看看它是否有效,并且无法在网上找到任何可以简明解释这一点的内容。 When I include it, nothing related to this div shows up in the console, if that's helpful.当我包含它时,控制台中不会显示与此 div 相关的任何内容(如果有帮助的话)。

here's the html:这是html:

<button class="btn btn-light" data-toggle="collapse" data-target="#everydaycollapse" aria-expanded="false" aria-controls="everydaycollapse">
  Everyday</button> 
<div class="collapse" id="everydaycollapse">
  <div class="card card-block" id="everydayContent">
      <div id="everdayFoodContent">Food</div><br><br>
      <div id="everdayDrinkContent">Drink</div>
  </div>
    </div>

It appears to be a simple spelling error.这似乎是一个简单的拼写错误。 You spelled the ID of the div in question " everday FoodContent", and you're trying to append it with the id " everyday FoodContent" in the jQuery call.你拼写div的问题“锁具FoodContent”的ID,和你想用jQuery的呼叫ID“天天FoodContent”追加它。 Note the 'y'.注意“y”。

Changing your append to this will resolve the issue:更改您的附加内容将解决此问题:

 $('#everdayFoodContent').append('<p>hello!</p>');

Or, alternately, you can revise your div ID to have the correct spelling like so:或者,您也可以修改您的 div ID 以获得正确的拼写,如下所示:

<div id="everydayFoodContent">Food</div><br><br>

Suppose you just need to decide on how you want to spell it!假设您只需要决定如何拼写它! ;) ;)

EDIT编辑

I've attached the full code of both the working HTML and CSS, so perhaps it will be easier.我已经附上了工作 HTML 和 CSS 的完整代码,所以也许它会更容易。 Copy and paste these into your project.将这些复制并粘贴到您的项目中。 If it works, then there was some other syntax mistake in your code.如果它有效,那么您的代码中还有其他一些语法错误。 If not, then you may be loading jQuery into your file incorrectly.如果没有,那么您可能错误地将 jQuery 加载到您的文件中。

HTML HTML

<button  class="btn btn-light" data-toggle="collapse" data-target="#everydaycollapse" aria-expanded="false" aria-controls="everydaycollapse">Everyday</button> 
<div class="collapse" id="everydaycollapse">
    <div class="card card-block" id="everydayContent">
        <div id="everydayFoodContent">Food</div><br><br>
        <div id="everdayDrinkContent">Drink</div>
    </div>
</div> 

jQuery jQuery

$(document).ready(function(){
    $('button').click(function(){
        console.log("button clicked");
        $('#everydayFoodContent').append('<p>hello!</p>');
    });    
})

I've verified that this code will successfully append the string whenever the button is clicked.我已经验证,无论何时单击按钮,此代码都会成功附加字符串。 It really should work in your browser.它真的应该在您的浏览器中工作。 If not, it's probably going to be a missing dependency or something else on your environment.如果没有,则可能是缺少依赖项或环境中的其他内容。

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

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