Friday, 15 August 2014

jQuery - If first element has class then Function -


I want to add a piece of code to affect all my pages.

Correct attribute ('id') == 'filter option') ($ ('$ container & gt; div: first') {$ ('# container'). Prepaid ('& lt; Div class = "banner">

& gt; ';);}

Output:

  

Example , if the container looks like this:

  & lt; Div id = "container" & gt; div class = "current banner"> 
& lt ; / Div & gt;

Nothing will be like the first filter in the filter I have hit a collision now because I have something that looks like this banner

  & lt; div id = "container" & Gt; A class = "current banner" href = "#" & gt; & lt; / a & gt; & lt; div id = "filterOptions" & gt; & lt; div & Gt; & lt; / Div & gt;  

This will be essentially a double banner that I want to save. Because it's an anchor instead of a div.

So my question is how do I target all elements instead of targeting div: first ?

Something like this ...

if ('$' ('# container') Anyone : first '). Attr (' id ') ==' filterOptions') {...}

You must use

  ($ ('($ (' # '(' # id ') ==' filter option ') { $ ('# Container'). Prepaid ('

Or you have a functional approach You can use:

  // var $ with container = $ ('# container'); ($ Container.children (). First (). Attr ('id') == 'filterOptions') {$ container.prepend (' & lt; div class = "banner"> ;);}  < / Pre> 

You can also flip around the argument:

  // filterOptions is the first child: if ($ ('$ filterOptions') .prev () Length === 0) {$ ('Container'). Prepaid ('

No comments:

Post a Comment