簡體   English   中英

如果在頁面中找到單詞“ Cheese”,則更改了windows.location.href目錄的jQuery代碼

[英]jQuery code that changed the windows.location.href directory if the word 'Cheese' found in the page

如何在div內搜索h1元素,該div具有包含標題“ Cheese”的類的.title-bar? 如果找到了Cheese,則添加一個單擊事件以重定向到abcx.com/cheese。

到目前為止,這是我嘗試過的:

jQuery(document).ready(function(jQuery){
    jQuery("span.xoo-cp-close.xoo-cp-icon-cross")
        .contains('.title-bar h1' "Cheese")
        .attr( "onClick", "window.location.href('abcx.com/cheese')" );
});

謝謝你的幫助

假設您的html標記如下所示。

<span class="xoo-cp-close xoo-cp-icon-cross">
  <div class="title-bar">
    <h1>Cheese</h1>
  </div>
</span>

您可以像這樣在文檔上簡單地迭代所有h1 ..

jQuery(document).ready(function() {
  jQuery(".title-bar h1", "span.xoo-cp-close.xoo-cp-icon-cross").each(function() {
    var $h1 = jQuery(this),
      h1Text = $h1.text().trim().toLowerCase();

    if (h1Text.indexOf("cheese") > -1) {
      $h1.attr("onClick", "window.location.href('abcx.com/cheese')");
    }
  });
});

上面的代碼使用.title-bar類迭代元素中的所有h1 ,該類是span元素的后代,該span元素同時包含xoo-cp-closexoo-cp-icon-cross

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM