简体   繁体   English

学习Vanilla JavaScript,进行循环

[英]Learning Vanilla JavaScript, for loop

I am trying to learn Vanilla JavaScript or pure JS however you look at it. 我正在尝试学习Vanilla JavaScript或纯JS,但是您会看一下。 I started off learning jQuery and now I am trying to better my speeds with no framework. 我从学习jQuery开始,现在尝试在没有框架的情况下提高速度。 I usually can get this with just jQuery but am having a hard time doing this with pure JS. 我通常可以使用jQuery来获取它,但是使用纯JS很难做到这一点。

var a = $('.entry-content'),i;

for (i=0;i<a.length; i++) {
   var b = a[i].innerHTML;
   var c = document.createElement('div');
   var d = c.id = 'reply_bb';
   var e = d.innerHTML = 'Reply';
   a[i].innerHTML = a[i].appendChild(c);
 }

Basically what I would like to do is add an element to the var a element. 基本上我想做的就是在var中添加一个元素。

   <div class="entry-content">
     <div>
         Words from a poster or whatever would be in side here
     </div>
   </div>

After javascript = 在javascript =之后

   <div class="entry-content">
     <div>
         Words from a poster or whatever would be in side here
     </div>
      <div id="reply_bb">Reply</div>
   </div>

Also since this is related. 因为这是相关的。 When creating an element dynamically would I use .click or .on('click',function() { or how would I go about that? I want it so when the user clicks reply_bb it appends another Element that is already on the page just hidden. 如果动态地创建一个元素,我会用.click.on('click',function() {或者我将如何去说?我想它,以便当用户点击reply_bb它附加其他元素已经在页面上只隐。

It starts to go wrong here: 它开始出错了:

var d = c.id = 'reply_bb';
var e = d.innerHTML = 'Reply';

d at this point is the string 'reply_bb' . d是字符串'reply_bb'

So d has no property .innerHTML . 因此d没有.innerHTML属性。


Then, I have no idea what you're trying to do with this: 然后,我不知道你要用这​​个做什么:

a[i].innerHTML = a[i].appendChild(c);

You either set the innerHTML or you append a child. 您可以设置innerHTML也可以追加子项。 It doesn't make sense to try to set the innerHTML to the result of appending a child (which is the DOM element that was appended). 尝试将innerHTML设置为附加子项(即附加的DOM元素)的结果是没有意义的。


It looks to me like perhaps what you want is this: 在我看来,您可能想要的是:

var items = $('.entry-content'), replyDiv;

for (var i = 0; i < items.length; i++) {
   replyDiv = document.createElement('div');
   replyDiv.className = 'replyButton';
   replyDiv.innerHTML = 'Reply';
   items[i].appendChild(replyDiv);
}

And, if you really want to do this without jQuery, you can do this: 而且,如果你真的想在没有jQuery的情况下这样做,你可以这样做:

var items = document.getElementsByClassName('entry-content'), replyDiv;

for (var i = 0; i < items.length; i++) {
   replyDiv = document.createElement('div');
   replyDiv.className = 'replyButton';
   replyDiv.innerHTML = 'Reply';
   items[i].appendChild(replyDiv);
}

In answer to your question about .on() vs .click() , it depends. 在回答你的问题有关.on() VS .click()它依赖。 These two are equivalent: 这两个是等价的:

$(item selector).click(fn) 
$(item selector).on('click', fn)

Both will work fine on items that exist at the time you install the event handler. 两者都可以在安装事件处理程序时存在的项目上正常工作。 I use .click() here just because it's more concise. 我在这里使用.click()只是因为它更简洁。

But, if the items you want the event handler on don't exist yet or will get created in the future, then you have to use delegated event handling where you install the event handler on a parent that exists already and you cannot do that with .click() so you'd use .on() like this: 但是,如果您希望事件处理程序所在的项目尚未存在或将来会创建,那么您必须使用委派事件处理,在已存在的父项上安装事件处理程序,而您不能使用.click()所以你要像这样使用.on()

$(static parent selector).on('click', '.replyButton', function() {
    // event handler code here
})'

A few other suggestions: 其他一些建议:

  1. Use meaningful variable names like replyDiv and items , not a , b , c , and d . 使用有意义的变量名称,如replyDivitems ,而不是abcd
  2. Don't create intermediate variables when there is no need. 不需要时不要创建中间变量。
  3. id values must be unique so either generate a unique id name or use a class name instead (my code switched to a class name). id值必须是唯一的,因此要么生成唯一的id名称,要么使用类名称(我的代码切换到类名)。

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

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