简体   繁体   English

多个选择器寻找同级

[英]Multiple Selectors looking for sibling

I am trying to look for the nearest sibling using multiple selectors.. but I keep getting each of my items adjusting the count when I just want the closest span, span. 我正在尝试使用多个选择器来寻找最接近的兄弟姐妹。但是当我只想要最接近的跨度时,我会不断让每个项目调整计数。

FIDDLE HERE: http://jsfiddle.net/70o9u96s/1/ 在这里: http : //jsfiddle.net/70o9u96s/1/

$("#SchedulingComment, #DiagramComment, #InspNotes, #PreInspHoursNotes, #ActualInspNotes, #HoursNotes").keyup(function(){

    var activeItem = $(this).attr('id');

    var count = $("#" + activeItem).text().length;
    var adjust = 255 - count;
    var placeCount = $("#" + activeItem).parent().find("span span");
    var colorCount = $("#" + activeItem).parent().find("span");

    placeCount.text(adjust);

    if (adjust < 0){
        colorCount.css("color","red");
    } else {
        colorCount.css("color","black");
    }

});

You have several errors, from not referencing the element being clicked on with this properly, to not getting the hierarchy correct. 你有几个错误,从不引用元素被点击与this正确,不得到正确的层次结构。

$("#SchedulingComment, #DiagramComment, #InspNotes, #PreInspHoursNotes, #ActualInspNotes, #HoursNotes").keyup(function () {
    var count = $(this).val().length;
    var adjust = 255 - count;
    var placeCount = $(this).prevAll('label:first').find("span span");
    var colorCount = $(this).prevAll('label:first').find("span");
    placeCount.text(adjust);
    if (adjust < 0) {
        colorCount.css("color", "red");
    } else {
        colorCount.css("color", "black");
    }
});

jsFiddle example jsFiddle示例

  • var activeItem = $(this).attr('id'); is unnecessary as $(this) is the way to refer to the textarea being typed in 不需要,因为$(this)是引用键入文本区域的方法
  • With textareas you want the value, not the text, so use .val() instead of .text() 对于textareas,您需要值而不是文本,因此请使用.val()而不是.text()
  • According to your code, the parent of the textareas would be the body, so what you want instead is to select the previous label element with .prevAll('label:first') 根据您的代码,textareas的父级将是正文,因此,您要选择的是使用.prevAll('label:first')选择先前的label元素。

I'd say your structure is not very good. 我会说你的结构不是很好。

I've changed it to this: 我将其更改为:

<div class="js-length-monitor-wrapper">
    <label>SchedulingComment: <span class="js-length-monitor-info"><span class="js-length-monitor-counter">255</span> characters remaining.</span></label>
    <textarea id="SchedulingComment" cols="30" rows="3" class="js-length-monitor"></textarea>
    </div>
<div class="js-length-monitor-wrapper">
    <label>SchedulingComment: <span class="js-length-monitor-info"><span class="js-length-monitor-counter">255</span> characters remaining.</span></label>
    <textarea id="DiagramComment" cols="30" rows="3" class="js-length-monitor"></textarea>
</div>

Then changed javascript to: 然后将javascript更改为:

$(".js-length-monitor").keyup(function(){
    var count = $(this).val().length;
    var adjust = 255 - count;
    console.log(adjust);
    var placeCount = $(this).prev('label').find(".js-length-monitor-counter");
    var colorCount = $(this).prev('label').find(".js-length-monitor-info");

    placeCount.html(adjust);

    if (adjust < 0){
        colorCount.css("color","red");
    } else {
        colorCount.css("color","black");
    }

});

Still it is not the best approach, since it relies on DOM structure, but it's a starter. 仍然它不是最佳方法,因为它依赖于DOM结构,但这是一个入门。

See it in action on jsfiddle . 可以在jsfiddle上看到它。

There are several things going on here that should be fixed. 这里有几件事应该修复。

1. parent() means something different than you think. 1. parent()的含义与您想象的有所不同。

Check out the documentation . 查看文档 In the HTML you provided in the fiddle, none of the textareas have a parent other than the body of the html... they all share the same parent! 在小提琴中提供的HTML中,除html的正文外,其他文本区域都没有父级...它们都共享同一父级!

<label>SchedulingComment: <span><span>255</span> characters remaining.</span></label><br />
<textarea id="SchedulingComment" cols="30" rows="3"></textarea>

If you want the SchedulingComment to have a relevant parent, you must wrap it in something. 如果希望SchedulingComment具有相关的父级,则必须将其包装在某些内容中。

<div class="input-section">
    <label>SchedulingComment: <span><span>255</span> characters remaining.</span></label><br />
    <textarea id="SchedulingComment" cols="30" rows="3"></textarea>
</div>

Now $("#schedulingComment").parent() means "look at the stuff inside the div called .input-section". 现在$(“#schedulingComment”)。parent()的意思是“查看div中名为.input-section的内容”。

2. Use the this keyword better 2.更好地使用this关键字

var activeItem = $(this).attr('id');
var count = $("#" + activeItem).text().length;

That's crazy. 太疯狂了。 Why not just do 为什么不做

var count = $(this).text().length;

It makes far more sense, and doesn't require any additional DOM lookups. 这更有意义,并且不需要任何其他DOM查找。

3. Speaking of which... text() doesn't work that way. 3.说到哪个... text()不能那样工作。

Use val() not text() for getting the string in the input box. 使用val() 而不是 text()在输入框中获取字符串。

4. Using selectors your way is painful when you update your html 4.更新HTML时,使用选择器很麻烦

When you change your html to be 当您将HTML更改为

<span><div>255</div></span>

... you're going to have to go update all of your javascript too. ...您也将必须更新所有JavaScript。 It's going to suck, and you're going to forget all the places where you put things. 它会很烂,您会忘记放置东西的所有地方。 It's MUCH better to use descriptive classes instead. 最好使用描述性类。 I would do something like this: 我会做这样的事情:

<div class="input-section">
    <label>SchedulingComment:
        <span class="character-count-holder">
            <span class="character-count">255</span>
            characters remaining.
        </span>
    </label><br />
    <textarea id="SchedulingComment" cols="30" rows="3"></textarea>
</div>

and my javascript would look something like: 和我的JavaScript将看起来像:

$(".input-section input").keyup(function(){
    var section = $(this.parent());
    var count = 255 - $(this).val().length;
    section.find(".character-count").text(count);
    if(count < 0) {
        section.find(".character-count-holder").css("color", "red");
    } else {
        section.find(".character-count-holder").css("color", "red");
    }
});

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

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