我有一個jQuery函數來更改網頁上的href。如何讓這個腳本只在#container
div內運行?使jQuery函數只能在特定的div ID或類中運行
$('a[href*="example.com"]').each(function(){
var index = this.href.indexOf(".com/");
this.href = this.href.slice(0, index+5)
})
我試過這個,
$('#container').$('a[href*="example.com"]').each(function(){
var index = this.href.indexOf(".com/");
this.href = this.href.slice(0, index+5)
})
,但它不工作。上面的代碼有什麼問題?
您只能使用一個$(..)。一旦你有一個jQuery對象,你繼續鏈式語句:$('#container')。someFunction()。someOtherFunction()... – frenchie
在它之前,我已經嘗試了每條語句的$()。像這樣$('#container')。('a [href * =「example.com」]'),但是它一樣,不起作用。 –
@frenchie是否可以使用**('#container','a [href * ='example.com')**? –