开发者

jQuery find if a dynamic generated <p> have text

开发者 https://www.devze.com 2022-12-20 03:50 出处:网络
I try to find if a dynamic created <p> have text. \'.layer\'+count returns myclass but I can not get it to work with .lenght.

I try to find if a dynamic created <p> have text. '.layer'+count returns my

class but I can not get it to work with .lenght.

if ( ($('.layer'+count).text()).length > 0 ){
alert ('I have text')
}

Any tips on making this开发者_StackOverflow社区 work? Thanx


if ( ($('.layer'+count).text().length ) > 0 )
{
    alert ('I have text')
}

In your code the paranthesis order was not the right one. It should be

($('.layer'+count).text().length)

and not

($('.layer'+count).text()).length

Actually there is no need of the extra (. You can simply write

if ( $('.layer'+count).text().length > 0 )
{
    alert ('I have text')
}


The extra parenthesis aren't needed around $('.layer'+count).text() but it should work nevertheless. Are you absolutely sure you selector matches the element? You can test that by using alert($('.layer'+count).length), that would alert the amount of elements matched.


What happens when you do:

alert($('.layer'+count).text())
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号