[英]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.