在每个函数中使用 jQuery 索引

发布于 2024-11-19 05:08:35 字数 1865 浏览 1 评论 0原文

我实际上有两个问题,

首先,我想计算 html 页面中的百分比并通过附加它用 javascript 返回它。这工作正常,但它在各处设置相同的值。我想我需要类似“这个”的东西,但我找不到如何让它发挥作用。 each 函数并不停留在 is li 内,而是在任何地方设置 it。

另一个问题是 var getalfavconvert = parseFloat(getalfav.substring(1)); 出于某种原因它返回一个奇怪的值。另一方面, var getalconvert = parseFloat(getal.substring(1)); 工作正常。我认为这与链接有关。

另一个问题是

$(".thing-shot ul").prepend('<li class="hammer">10%</li>');

$('.things li .group').each(function(index) {

    // get value from page
    var getal = $('.current-user-view').text();
    var getalfav = $('.fav.marked a').text();

    // convert value to float
    var getalconvert = parseFloat(getal.substring(1));   
    var getalfavconvert = parseFloat(getalfav.substring(1));   

    //make a percentage calculation
    var gedeeld = getalconvert/getalfavconvert;
    var percentage = gedeeld*100;

    // Set percentage in html
    $(".hammer").text(percentage);        
});

html中li的结构:

<li id="screenshot-2081" class="group">
    <div class="athing">
        <div class="thing-shot">
            <div class="thing-img">
                <a href="go.html" class="link"><img alt="teaser" src="teaser.png?1310142565" /></a>
            </div>

            <ul class="tools group">
                <li class="fav marked">
                    <a href="go.html">21</a>
                </li>
                <li class="cmnt current-user-cmnt
                    ">
                    <a href="#">6</a>
                </li>
                <li class="views current-user-view">
                    276
                </li>
            </ul>
        </div>
        <div class="extras">

        </div>
    </div>
</li>

I have actually 2 questions,

First, I want to calculate a percentage in a html page and return it with javascript by appending it. This works fine but it sets everywhere the same value. I think I need something like 'this' but I can't find how to get it to work. The each function doesn't stay within is li, but sets its everywhere.

Another problem is the var getalfavconvert = parseFloat(getalfav.substring(1)); for some reason it returns a strange value. The var getalconvert = parseFloat(getal.substring(1)); on the other hand works fine. I think it has something to do with the a link.

Another problem is

$(".thing-shot ul").prepend('<li class="hammer">10%</li>');

$('.things li .group').each(function(index) {

    // get value from page
    var getal = $('.current-user-view').text();
    var getalfav = $('.fav.marked a').text();

    // convert value to float
    var getalconvert = parseFloat(getal.substring(1));   
    var getalfavconvert = parseFloat(getalfav.substring(1));   

    //make a percentage calculation
    var gedeeld = getalconvert/getalfavconvert;
    var percentage = gedeeld*100;

    // Set percentage in html
    $(".hammer").text(percentage);        
});

Here is the structure of li in the html:

<li id="screenshot-2081" class="group">
    <div class="athing">
        <div class="thing-shot">
            <div class="thing-img">
                <a href="go.html" class="link"><img alt="teaser" src="teaser.png?1310142565" /></a>
            </div>

            <ul class="tools group">
                <li class="fav marked">
                    <a href="go.html">21</a>
                </li>
                <li class="cmnt current-user-cmnt
                    ">
                    <a href="#">6</a>
                </li>
                <li class="views current-user-view">
                    276
                </li>
            </ul>
        </div>
        <div class="extras">

        </div>
    </div>
</li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-11-26 05:08:35

您遇到的“到处改变一切”问题是很常见的,因为您选择了某个类的所有对象。您想要做的是在使用 .each() 时通过使用 $(this) 找到每个对象的子级,使用 .children() 选择器。

我已经编辑了您的 jQuery 以反映这些更改:

$(".shot ul").prepend('<li class="hammer">0%</li>');

$('.things li .group').each(function(index) 
{
    // get value from page
    var getal = $(this).children('.current-user-view').text();
    var getalfav = $(this).children('.fav.marked').find("a").text();

    // convert value to float
    var getalconvert = parseFloat(getal.substring(1));
    var getalfavconvert = parseFloat(getalfav.substring(1));

    //make a percentage calculation
    var gedeeld = getalconvert/getalfavconvert;
    var percentage = gedeeld*100;

    // Set percentage in html
    $(this).children(".hammer").text(percentage); 
});

编辑

如果您查看上面的代码,您会看到

var getalfav = $(this).children('.fav.marked' ).find("a").text();

这里的关键是 .find("a") - 实际的 标签不是以下的孩子

    ,但是 .children() 不喜欢这个子子项,因此使用 .find()。另一种方法是这样的:

var getalfav = $(this).children('.fav.marked').text();

这仍然给出了正确的值,因为 .text( ) 删除内容中的所有 HTML,只留下一个数字。我推荐 .find() 方法,因为它更容易阅读并且语义正确,而且任何其他添加的内容都会把事情搞砸。

看一下 这个 JSFiddle 的示例:-)

The 'changing everything everywhere' problem you have is quite a common one, as you're selecting all objects of a certain class. What you want to do is find the children of each object when using .each() by using $(this), using the .children() selector.

I've edited your jQuery to reflect these changes:

$(".shot ul").prepend('<li class="hammer">0%</li>');

$('.things li .group').each(function(index) 
{
    // get value from page
    var getal = $(this).children('.current-user-view').text();
    var getalfav = $(this).children('.fav.marked').find("a").text();

    // convert value to float
    var getalconvert = parseFloat(getal.substring(1));
    var getalfavconvert = parseFloat(getalfav.substring(1));

    //make a percentage calculation
    var gedeeld = getalconvert/getalfavconvert;
    var percentage = gedeeld*100;

    // Set percentage in html
    $(this).children(".hammer").text(percentage); 
});

EDIT

If you look at the code above, you'll see

var getalfav = $(this).children('.fav.marked').find("a").text();

The key here is .find("a") - the actual <a> tag isn't a child of the <ul>, but a sub-child which .children() doesn't like, hence the .find(). Another way to do it is this:

var getalfav = $(this).children('.fav.marked').text();

This still gives the correct value because .text() strips all HTML from the contents and just leaves you with a number. I'd recommend the .find() method though as it's easier to read and semantically correct, as well as the fact that any other added content would mess things up.

Take a look at this JSFiddle for an example :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文