开发者

Use jquery ':contains' to find specific javascript within a span

This is my first time here, I hope that this is clear.

So i will have code similar to this this.

 <span class="mediaSource ui-draggable" id="purchsePlay7915504">
    <a href="" onclick="return popup_window(this, 'MediaView', 850, 680)" class="control"  enter code hereid="GenericLink"></a>
    <img id="Any_71" alt="Media Source" src="images/9672web.gif" class="mediaStationIcons mediaWin"/>
    <img class="player" alt="Media Source" src="images/playmedia.gif" style="display: none;"/>
    </span>  

The href portion is generated on the backend, and I have no access to it.

I need to modify some existing jquery code to do something based on what the 'onclick' function is(there are different ones e.g. popup_window1,popup_window2 etc.) .

I tried something l开发者_开发问答ike this:

$('.segmentLeft span.mediaSource').click(function(){
if ($('span:contains("popup_window")').length > 0) {
 do something
}
});

but it does not seem to work.


Try $('span > a[onclick*="popup_window"]');


:contains applies only to text within a specific element. If the text "popup_window" is not literally spelled out within the node span, it's not going to apply.

I also noticed that your if check checks the general span, instead of the local span. The difference is that when you click span.mediaSource, the click is going to apply to any span that has a popup_window element within it. To check the local span, you would want to use this, as in $(this).children('span:contains("x")')

You need to know some attribute, class or id of the element you want to select in order to select the entire node. Using the name of the function won't work. Also, if the element you want to select is generated dynamically through JS (which it appears to be), you also have to make sure that element is loaded before you send your script off to try to select it, otherwise you may run into issues with asynchronicity.


First select the a tag correctly, then in the click look at the attribute's value.

$(document).ready(function() {

  $('span.mediaSource a.control').click(function(e) {

    if ($(this).attr('onclick').indexOf('popup_window') {
      //Do Stuff
    }

    e.preventDefault(); //Cancel the click action
  });

});

There is no need to use the selector to check the onclick attribute, it will be very slow as the selector engine may need to check multiple elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜