javascript - 如何动态附加jquery函数

标签 javascript html jquery jquery-selectors

是否可以在另一个之后动态附加 jquery 函数..

这是我尝试过的

$('div#bottomPart').children().getNext(3).children().children().find('p').eq(3).text();

function getNext(num) {
    var appendNext = "";
    for (var i = 0; i < num; i++) {
        appendNext = appendNext + next();
    }
    return appendNext;
}

不要评判代码,它只是为了让大家了解我的想法。

我正在尝试在选择器中使用 for 循环添加 next(),例如

如果 getNext(1) 那么它应该返回像 $('div#bottomPart').children().next().children().children().find('p') 这样的选择器。 eq(3).text();

或者如果 getNext(2) 那么它应该返回像 $('div#bottomPart').children().next().next().children().children().find( 'p').eq(3).text();

这是我的 HTML:

<div id="bottomPart">
    <div class="card d-flex flex-row mb-3 table-heading">
        <div class="d-flex flex-grow-1 min-width-zero">
            <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center" id="compareHeading">
                <p class="list-item-heading mb-0 truncate width-30 w-xs-100">Parameters</p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">
                    <img class="card-img-top" style="width: 7rem; object-fit: contain;" src="/static/images/Go Digit General Insurance Company Ltd..jpg" alt="Go Digit General Insurance Company Ltd.">
                </p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">
                    <img class="card-img-top" style="width: 7rem; object-fit: contain;" src="/static/images/Reliance General Insurance Company Ltd..jpg" alt="Reliance General Insurance Company Ltd.">
                </p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">ENTERPRISE</p>
            </div>
        </div>
    </div>
    <div class="card d-flex flex-row mb-3">
        <div class="d-flex flex-grow-1 min-width-zero">
            <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center">
                <p class="list-item-heading mb-0 truncate width-30 w-xs-100">Own Damage Cover</p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">od1</p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">od2</p>
                <p class="mb-0 text-primary w-20 w-xs-100 text-center">od3</p>
            </div>
        </div>
    </div>
    <div class="card d-flex flex-row mb-3">
        <div class="d-flex flex-grow-1 min-width-zero">
            <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center">
            <p class="list-item-heading mb-0 truncate width-30 w-xs-100">Team permissions</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">tp1</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">tp2</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">tp3</p>
        </div>
    </div>
</div>
<div class="card d-flex flex-row mb-3">
    <div class="d-flex flex-grow-1 min-width-zero">
        <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center">
            <p class="list-item-heading mb-0 truncate width-30 w-xs-100">24/5 Support</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su51</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su52</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su53</p>
        </div>
    </div>
</div>
<div class="card d-flex flex-row mb-3">
    <div class="d-flex flex-grow-1 min-width-zero">
        <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center">
            <p class="list-item-heading mb-0 truncate width-30 w-xs-100">24/7 Support</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su71</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su72</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">su73</p>
        </div>
    </div>
</div>
<div class="card d-flex flex-row mb-3">
    <div class="d-flex flex-grow-1 min-width-zero">
        <div class="card-body align-self-center d-flex flex-column flex-md-row justify-content-between min-width-zero align-items-md-center">
            <p class="list-item-heading mb-0 truncate width-30 w-xs-100">User actions audit log</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">log1</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">log2</p>
            <p class="mb-0 text-primary w-20 w-xs-100 text-center">log3</p>
        </div>
    </div>
</div>

因为我正在使用 for 循环(不是上面的循环)根据用户提供的数据在我的 html 中找到 p 标签。

如果有一个附加 next() 函数的解决方案,那对我来说会容易得多..

那有可能吗??

最佳答案

看看这是否对你有帮助:

function getNext(selectorJquery, methodsValues) {
    var next = $(selectorJquery);
    $(methodsValues).each(function() {
        for (var method in this) {
            if (this[method] !== null) {
                next = next[method](this[method]);
            } else {
                next = next[method]();
            }
        }
    });

    return next;
}

var methodsValues = [
    {children: null},
    {next: null},
    {children: null},
    {children: null},
    {find: 'p'},
    {eq: 3},
    {text: null}
];

console.log($('div#bottomPart').children().next().children().children().find('p').eq(3).text());
console.log(getNext($('div#bottomPart'), methodsValues));

var otherMethodsValues = [
    {children: null},
    {next: null},
    {next: null},
    {children: null},
    {children: null},
    {find: 'p'},
    {eq: 3},
    {text: null}
];

console.log($('div#bottomPart').children().next().next().children().children().find('p').eq(3).text());
console.log(getNext('div#bottomPart', otherMethodsValues));

关于javascript - 如何动态附加jquery函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174823/

相关文章:

javascript - Javascript 表单与 Ajax 一致工作以登录到私有(private) Magento 商店的问题

javascript - 倒计时和重定向的正确方法

javascript - 未捕获的类型错误 : Cannot read properties of undefined (reading 'deep' ) when upgrade to vue 3. x

html - 选择在 FireFox 中不显示为正常

html - html标签为空时如何隐藏

javascript - 反转字符串中的单词

php - ajax 类似结构

javascript - 下载文件时禁止打开弹出窗口

javascript - 按键事件在 ionic 框架中不起作用

html - 如何制作可点击框