简体   繁体   English

如何在以前使用div的脚本中合并要调用的列表项?

[英]How to incorporate a list item to be called on in a script that previously used a div instead?

I have an unordered list that contains multiple list items (represented by green boxes). 我有一个无序列表,其中包含多个列表项(以绿色框表示)。

Each list item contains a div called "titlebox" which you can see in my screenshot below. 每个列表项都包含一个名为“ titlebox”的div,您可以在下面的屏幕快照中看到它。 It says "Top Line Shorter Than Bottom". 它说“顶线比底线短”。

Screenshot: 截图: 在此处输入图片说明

You can see how the top line is shorter than the bottom. 您可以看到顶线比底线短。 This is done using the script posted below. 这是使用下面发布的脚本完成的。 However, this script calls on "titlebox" in order to work. 但是,此脚本调用“ titlebox”才能正常工作。 Long story short, I updated my site to use list items instead of divs to produce the title, but simply substituting "titlebox" with "li" in the script doesn't produce the desired result. 长话短说,我更新了我的网站以使用列表项而不是div来生成标题,但是仅在脚本中用“ li”替换“ titlebox”并不能产生理想的结果。

Here is the code for the current list item (including titlebox div): 这是当前列表项(包括标题框div)的代码:

<li class="list__item2">
    <figure class="list__item__inner">  
    <p class="vignette" style="background-image:url(http://www.fakeimage.com/image.jpg)"></p>
    <div class="titlebox">Top Line Shorter Than Bottom</div>
       <div class="locationbox">Sample Title</div>
     <div class="pricebox">Sample Title</div>
</li>

and the script that makes top line shorter is: 而使顶行更短的脚本是:

 <script type='text/javascript'>
     $(document).ready(function() {
          $(".titlebox").each(function(){
  var len = $(this).text().length,
      words = $(this).text().split(" "),
      line1 = [],
      line2 = [],
      html = "";

  // iterate through each word in the title
  $.each(words, function(i,word){
    // if line 1's current length plus the length of this word
    // is less than half the total characters, add word to line 1
    // else add to line 2
    // (check index of word to maintain order)
    if((line1.join(" ")+" "+word).length < (len/2) && (i == line1.length)){
      line1.push(word);
    } else {
      line2.push(word);
    }
  });

  // concatenate the results with a '<br>' separating the lines
  html = line1.join(" ")+"<br>"+line2.join(" ");

  // replace the .titlebox content with this new html string
  $(this).html(html);
});
});
      </script>

But my NEW html code doesn't use titlebox div, instead it produces the list item (green box) using this code: 但是我的新html代码不使用titlebox div,而是使用以下代码生成列表项(绿色框):

<div class="block2 personal fl">
        <h2 class="title"></h2>
            <div class="content">
                    <p class="price">
                       <p class="vignette" style="background-image:url(http://www.fakeimage.com/image.jpg)"></p>
                    </p>

             </div>

                <ul class="features">
                    <li>Top Line Shorter Than Bottom</li>
                    <li>Sample Title</li>
                    <li>Sample Title</li>

                </ul>
</div>

I want the line <li>Top Line Shorter Than Bottom</li> to be called on within the script posted above. 我希望在上面发布的脚本中调用<li>Top Line Shorter Than Bottom</li> How do I do this? 我该怎么做呢?

Thank you! 谢谢!

You need to change your javascript to select the proper element: 您需要更改JavaScript以选择适当的元素:

$(".features li:first-child").each(function(){...

OR, if you have access to update the HTML, you can add a class to your list item, in which case you don't need to update your javascript at all: 或者,如果您有权更新HTML,则可以将一个类添加到列表项,在这种情况下,您根本不需要更新JavaScript:

<ul class="features">
    <!-- add the class to the first list item -->
    <li class="titleBox">Top Line Shorter Than Bottom</li> 
    <li>Sample Title</li>
    <li>Sample Title</li>
</ul>

<script type='text/javascript'>
    $(document).ready(function() {
        $(".titlebox").each(function(){
  ...

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

相关问题 如何从以前单击的列表项中删除样式 - How to remove style from previously clicked list item 显示浮动Div而不是dropdownlist的选择项内容列表 - Showing floating Div instead of dropdownlist's selection item content list 如何将Praat脚本合并到网页/数据库中 - how to incorporate praat script into webpage/database 我有一个用户脚本来更改嵌入的聊天颜色。 如何将脚本的功能合并到页面中? - I have a userscript to change embedded chat colors. How do I incorporate the script's functionality into my page instead? 如何在同一单击事件 Jquery 的无序列表中获取先前单击的列表项 - How do I get the previously clicked list item in an unordered list in same click event Jquery 如何将列表项连接到div元素javascript - How to connect list item to div element javascript 如何解开div,然后在单击时将div添加到列表中的下一项 - How to unwrap a div and then add a div to the next item in a list on a click 如何删除之前带有边框的项目 - How do I delete an item previously border 如何获取单个页面中使用/调用的javascript方法列表? - how to get list of javascript methods used/called in an individual page? 如何遍历无序列表,然后选择列表项的父div - How to iterate through an unordered list and then select the parent div of the list item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM