当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - find() not working properly. Cant find span

问题描述:

Trying to fill a span by finding it and using innterHTML but for some reason it doesn't do anything. No error, no succes, doesnt print anything. What has gone wrong?

<label class="myClass" data-id="1"><input type="checkbox" name="group" value="my_value">my value<span class="left" ></span></label>andmoreandmore

I've put it in a for loop for getting the classes

var element = $('.myClass')[i];

var numberThing = 50 - count;

$(element).find("span").innerHTML = numberThing + ' plekken vrij';

网友答案:

Well what has gone wrong is that you did not select the correct span, or better said.. none at all

You need to select span[0] because you are selecting a DOM element the wrong way.

var element = $('.myClass')[i];
var numberThing = 50 - count;
$(element).find("span")[0].innerHTML = numberThing + ' plekken vrij';
网友答案:

Use html() instead of innerHTML

var element = $('.myClass');
var count = 3;
                var numberThing = 50 - count;
                $(element).find("span").html(numberThing + ' plekken vrij');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label class="myClass" data-id="1"><input type="checkbox" name="group" value="my_value">my value<span class="left" ></span></label>andmoreandmore
网友答案:

Just use .eq() instead of []. Because $('.myClass')[i] will return a dom element. which means for using the jquery methods, you need to wrap it with $() again. Instead of these, you can use,

$('.myClass').eq(i).find("span").html(numberThing + ' plekken vrij');
分享给朋友:
您可能感兴趣的文章:
随机阅读: