简体   繁体   English

使用.text()抓取父文本

[英]Grabbing parent text using .text()

I am having trouble getting the text 'Vegetable' from the DOM 我无法从DOM中获取文本“ Vegetable”

   <div class="input-group">
    <ul>
        <li>
            <span class="input-checkbox">
                <input type="checkbox" aria-label="...">
            </span>
            Vegetable
        </li>
     </ul>
    </div>

Currently, I have an event handler for when I check the box to grab the text of the element of the list, but what I am doing is not working: $(e.target).parent().text() . 当前,我有一个事件处理程序,用于当我选中该框以获取列表元素的文本时,但我正在执行的操作不起作用: $(e.target).parent().text() My thinking was that I am targeting "input-checkbox" , so i need to get the parent and then grab the text of it. 我的想法是我的目标是"input-checkbox" ,所以我需要获取父项,然后获取其文本。 Is that the right approach or is it something wrong with my jQuery? 这是正确的方法还是我的jQuery有问题?

Events Code: 活动代码:

Template.produceList.events({
    'click .input-checkbox': function(e) {
        e.preventDefault();
        console.log('clicked on filter');
        var text = $(e.target).parent().text();
        return;
    }
});

You need to go two levels up because "input" is inside "span" which is inside "li", and the text "Vegetable" is inside "li". 您需要向上两个级别,因为“输入”位于“ li”内部的“ span”内部,而文本“ Vegetable”位于“ li”内部。

$(e.target).parent().parent().text()

I have created an example here (it's a little modified form of your code): https://jsfiddle.net/x5yb2m30/ 我在这里创建了一个示例(对您的代码进行了一些修改): https : //jsfiddle.net/x5yb2m30/

Another way to do this would be to use parents() and look for li tag: 另一种方法是使用parent()并查找li标签:

$(e.target).parents("li").text()

With jQuery this is the element the event is registered against, the target is the DOM node that acted on the event. 使用jQuery, this是事件要针对其注册的元素,目标是对事件进行操作的DOM节点。 In this case, the event.target is the input field itself. 在这种情况下, event.target是输入字段本身。

// this will give you the expected container 
// regardless of the child element the target belongs to
$(event.target).closest('li');

As an aside, I suggest the following structure for checkboxes and radio buttons. 顺便说一句,我建议复选框和单选按钮的结构如下。

<label>
  <input type="checkbox" ... />
  <span>Text Goes Here</span>
</label>

This way, the label itself will target the input for clicking... beyond this, you can use an adjacent sibling selector to override display behavior if you're using a graphical font like fontawesome for checkboxes 这样,标签本身将以点击输入为目标...除此之外,如果您使用诸如fontawesome之类的图形字体作为复选框,则可以使用相邻的兄弟选择器来覆盖显示行为。

label + input[type=checkbox] { display:none }
label > input[type=checkbox] + span:before { 
  /*setup for glyph font character*/
  font-family: 'FontAwesome';
  content: "\f096"; //square-o
  color:#000;
}
label > input[type=checkbox]:checked + span:before {
  content: "\f046"; //check-square-o 
}
label > input[type=checkbox]:disabled + span:before {
  color:#888;
}

Also, I would suggest using data- attributes on your selected container for whatever data you need access to, don't rely on the text itself. 另外,我建议您在所选容器上使用data-属性来获取需要访问的任何数据,不要依赖于文本本身。 Either have values and/or data on the input elements themselves, or the appropriate container. 在输入元素本身或适当的容器上具有值和/或数据。

ex: 例如:

<label data-category="foo">
    <input id="chkFoo" type="checkbox" value="bar" />
    <span>Foo</span>
</label>

You can access data via. 您可以通过访问数据。 $('#chkFoo').val() or $('#chkFoo').parent().data('category') which is easier to reason with, and can remain consistent regardless of display text, which may be localized in a given application. $('#chkFoo').val()$('#chkFoo').parent().data('category') ,更易于推理,并且无论显示文本如何都可以保持一致在给定的应用程序中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM