【问题标题】:looping thru div ids to display somewhere else循环通过 div id 显示在其他地方
【发布时间】:2014-05-07 20:33:34
【问题描述】:

我有一个使用 Location id 的 div,如下所示:

<div id="Weymouth">
    <ul style="padding: 4px 6px;">
        <li>
            <div style="float:left;padding: 0 0 0 0; text-align: left; font-size: 14px; font-weight: bold;">May 21st</div>
            <div style="float:right;padding: 0 0 0 0; text-align: right;">5PM - 7:30PM</div>
            <div style="clear: both"></div>
            <div style="float:left;padding: 0 0 0 0; text-align: left;">Weymouth Liquor Store</div>
            <div style="float:right;padding: 0 0 0 0; text-align: right;"><a href="https://www.google.com/maps/dir/Andover,+MA//@42.6495835,-71.166032,12z/data=!4m8!4m7!1m5!1m1!1s0x89e308597f9a4cf5:0x28f59c5c00d57256!2m2!1d-71.1367953!2d42.6583356!1m0" class="amstel" target="_blank">Directions</a></div>
            <div style="clear: both"></div>
        </li>
    </ul>
</div>
<div id="Marshfield">
    <ul style="padding: 4px 6px;">
        <li>
            <div style="float:left;padding: 0 0 0 0; text-align: left; font-size: 14px; font-weight: bold;">May 10th</div>
            <div style="float:right;padding: 0 0 0 0; text-align: right;">6PM - 8PM</div>
            <div style="clear: both"></div>
            <div style="float:left;padding: 0 0 0 0; text-align: left;">Marshfield Liquor Store</div>
            <div style="float:right;padding: 0 0 0 0; text-align: right;"><a href="" class="amstel">Directions</a></div>
            <div style="clear: both"></div>
        </li>
    </ul>
</div>

我将拥有 20 个这样的位置。我想在网页的另一部分显示它们:我通过调用 20 个位置中的每一个来使其工作,如下所示:

var weymouth = document.getElementById('Weymouth').innerHTML;
var marshfield = document.getElementById('Marshfield').innerHTML;  (do this for all 20 locations)

然后在 ul li 中调用它以稍微不同地显示它:

<ul>
    <li class="alt">
        <div style="padding: 0 0 0 0; text-align: left;">
            <script language="javascript" type="text/javascript">document.write(marshfield);</script>
        </div>
        <div style="clear: both"></div>
    </li>
    <li class="alt">
        <div style="padding: 0 0 0 0; text-align: left;">
            <script language="javascript" type="text/javascript">document.write(weymouth);</script>
        </div>
        <div style="clear: both"></div>
    </li>
</ul>

使用相同的 id(我想重命名为 Location)并将 _1 到 _20 附加到每个位置...如何将其放入循环并显示而无需显示 20 个变量和 20 li?


使用更新的代码附加到这篇文章,但仍然无法正常工作。任何帮助将不胜感激: 这是我的javascript:

   <script type="text/javascript">
        var target = document.querySelector('#target'), // get a reference to empty UL, target
        divs = ['Andover', 'Westford', 'Burlington', 'Boston', 'Weymouth', 'Marshfield'];
        //add ids of the divs that you want to copy to this array

        divs.forEach(function (v) { //iterate over ids array, and repeat below process for each id
            var el = document.querySelector('#' + v).cloneNode(true); //copy the div with "v" id
            var li = document.createElement('li') //create a new list item in memory
            li.className = 'alt'; //set css class name of new list item
            li.appendChild(el); // add the copied div to list item
            target.appendChild(li); //finally add the new list item to your target.
        });
    </script>

And here is my HTML:

        <div class="locations">
        <div class="headline">Burger Truck Locations</div>
             <ul id="target"></ul>
        </div>

 It is not displaying anything.  Need help.

【问题讨论】:

  • document.write 不是一个好主意。请改用innerHTML。我会在你的 ID div 上使用一个通用类。出于对 Pete Sake 的热爱,请从您的 HTML 中获取所有这些样式!

标签: javascript loops for-loop


【解决方案1】:

如果你只是想复制一些&lt;div&gt;&lt;ul&gt;,你可以做以下

<!-- create an empty <ul> in HTML to copy your div's into -->
<ul id="target"></ul>

在你的&lt;ul&gt;&lt;div&gt; 之后,运行这个脚本

var target = document.querySelector('#target'), // get a reference to empty UL, target
divs = ['Weymouth', 'Marshfield'];
//add ids of the divs that you want to copy to this array

divs.forEach(function (v) { //iterate over ids array, and repeat below process for each id
   var el = document.querySelector('#' + v).cloneNode(true); //copy the div with "v" id
   var li = document.createElement('li') //create a new list item in memory
   li.className = 'alt'; //set css class name of new list item
   li.appendChild(el); // add the copied div to list item
   target.appendChild(li); //finally add the new list item to your target.
});

附带说明,您应该使用 CSS 类而不是内联样式,并避免使用 document.write

【讨论】:

  • 对不起,我是新手,所以我为订单项中的样式道歉。但是你是说我应该做一个 ul (
      ) 然后在里面放一个
      吗?那么,不使用document.write怎么显示呢?
    • 您创建 HTML 文本或 DOM 元素(答案演示了如何)并将它们插入到您需要的元素中(target 答案)。关于 CSS 样式/类的一点是,您可以创建一个类,例如 .padd{padding: 4px 6px;} 并将该类应用到您的 ul 中,而不是写 style="padding: 4px 6px;" inline,而不是 &lt;ul class="padd"&gt; 而不是 &lt;ul style="padding: 4px 6px;"&gt;
    • 这不起作用。什么都不显示。我将在您的原始帖子中显示我现在所拥有的内容。
    • 没关系。在脚本上缺少一个结束字符。谢谢你的帮助。
    • 如果它解决了您的问题,您可以投票并接受答案。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签