简体   繁体   English

在创建的元素Pure JS中定位元素

[英]Targeting an element within a created element, Pure JS

I've been developing a snippet of code for a while now mostly using Pure JS I'm not to well versed on JS but I can walk my way around it within reason. 我一直在开发一小段代码,现在主要使用Pure JS,虽然我不精通JS,但是我可以在合理范围内解决它。 I'm trying to target an element of HTML that has been placed inside a JS created div. 我正在尝试定位放置在JS创建的div中的HTML元素。

The idea is to change the color of a character counter when it gets to 50 then finally 0. I have tried to implement the following snippet myself but I'm having difficulties trying to get it to work correctly. 这个想法是当字符计数器的颜色变为50时最终变为0,然后最终变为0。我试图自己实现以下代码段,但在使它正确工作方面遇到困难。

After some digging around the net I stumbled upon a question located here, Stack Overflow Question . 经过一番摸索之后,我偶然发现了一个位于此处的问题Stack Overflow Question Mentioning the need to dive deeper in sourcing the element thats being targeted Ie var targetDiv = document.getElementById("foo").getElementsByClassName("bar")[0]; 提到需要更深入地寻找目标对象,即var targetDiv = document.getElementById("foo").getElementsByClassName("bar")[0]; . However I feel it doesn't really apply to me as I'm not giving my created div an ID or using classes. 但是我觉得这并不真正适用于我,因为我没有给创建的div一个ID或使用类。

Here's what I tried to implement, 这是我尝试实现的

 // create wrapper element
  this.wrapper = document.createElement("div");
  this.wrapper.innerHTML = 'Chars left: <span id="int">' + (this.MAX - input.value.length), '</span>';

// The <span></span> element is the one I am trying to adjust.

// check counter value
var tail = document.getElementById("int");

if (this.MAX - this.INPUT.value.length <= 50) {
    tail.style.color = "#ffa";
} else if (this.MAX - this.INPUT.value.length <= 0) {
    tail.style.color = "#f00";
}

You can find the full code snippet I've been creating using JSFiddle . 您可以找到我使用JSFiddle创建的完整代码段。 Can anyone help me identify what I am missing here, or enlighten me as to if I'm approaching this wrong? 谁能帮助我确定我在这里所缺少的东西,或者启发我有关我是否正在解决这一错误?

Looks like you need to listen for when a character is entered into the input. 看起来您需要在将字符输入到输入中时进行侦听。 Those .value.length properties aren't updated live, so you'll need to check every time the user presses a key. 这些.value.length属性不会实时更新,因此您需要在用户每次按键时进行检查。

// create wrapper element
this.wrapper = document.createElement("div");
this.wrapper.innerHTML = 'Chars left: <span id="int">' + (this.MAX - input.value.length), '</span>';

// check counter value
var tail = document.getElementById("int");

// Inside the event handler function, the "this" keyword changes meaning to mean the element on which the event listener is attached. Redefining the variable here is one way to get max inside the handler.
var max = this.MAX;

this.INPUT.addEventListener('keyup', function () {
    if (max - this.value.length <= 50) {
        tail.style.color = "#ffa";
    } else if (max - this.value.length <= 0) {
        tail.style.color = "#f00";
    }
}

You might also want to check the logic of else if (max - this.value.length <= 0) - the length will never be less then zero :) Also, what do you want to happen when it goes above 50? 您可能还想检查else if (max - this.value.length <= 0)的逻辑, else if (max - this.value.length <= 0) -长度永远不会小于零:)另外,当长度超过50时,您想发生什么?

在此样式化:

TextAreaRanger.prototype["update"] = function() {.....}

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

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