4

私のウェブサイトIDで、URLに「test」という単語が含まれている場合にjQuery関数を実行したい

私がやろうとしているのは、私のURLに「rest」文字列が含まれている場合、ページ上の要素にマージンを追加することだけですか?

これまでに行ったことを示すために、jSfiddleを追加しました。

$(document).ready(function(){         
    // How can I check to see if my url contains the word 'TEST' then run the below function?
    $('.block-widget').css('margin-top, 252px')     
});
4

6 に答える 6

9

window.location現在地のURLを取得するために使用します。

条件に基づいて、margintopプロパティを適用できます。

$(document).ready(function(){         
     var pathname = window.location.pathname;
     if(pathname.indexOf('text') > -1){
        $('.block-widget').css('margin-top, 252px');
     }     
});
于 2012-09-21T16:17:09.483 に答える
2
$(document).ready(function(){         
   if (document.url.match(/test/g)){
     $('.block-widget').css('margin-top, 252px')     
  }
});
于 2012-09-21T16:18:31.757 に答える
0

URLについて知っておく必要のある詳細が含まれているこのリンクを見てください。

https://developer.mozilla.org/en-US/docs/DOM/window.location

$(document).ready(function(){         
    if (window.location.href.indexOf('rest') >= 0) {
      $('.block-widget').css('margin-top, 252px')     
    }
});
于 2012-09-21T16:19:15.243 に答える
0

パス名を取得します。

var pathname = window.location.pathname;

次に、「TEST」が含まれているかどうかを確認します。

if (pathname.toLowerCase().indexOf("test") >= 0)

于 2012-09-21T16:19:24.130 に答える
0

これを試して

$(document).ready(function(){         
    var patt=/test/g;
    if(patt.test(window.location.pathname)){
    $('.block-widget').css('margin-top, 252px')  
  }   

});
于 2012-09-21T16:19:47.273 に答える
0

name *='keyword'セレクターはtrueオプションです。

<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

<script>
$( "input[name*='man']" ).val( "has man in it!" );
</script>

リソース:https ://api.jquery.com/attribute-contains-selector/

于 2015-05-26T12:43:09.677 に答える