0

私のhtmlページには、 classを含む4つのarticleタグがありますcol。各記事の中には、divwith クラスがありheadingます。その中にimgタグをつけています。

各記事を反復処理して、そのイメージ タグを削除し、別の html に置き換えようとしています。

これはWordpressで、ファンキーなjquery関数ラッパーです。

現在、検索と置換を機能させようとしているだけです(以下)-各記事のビットごとに異なるコードをまだ試していません。以下が機能しない理由がわかりません。

(function ($) {
  $(document).ready(function() {
    $('article.col').each(function(el) {
      $((el).find('heading img').replaceWith('<newtag>somecode</newtag>'));  
    });
  });
 }(jQuery));
4

3 に答える 3

2

を呼び出すときeach、関数の値はfunction(index, element). だから今、あなたは で数値を検索しようとしています$(el)、試してみる$(this)か、関数に他の引数を追加してください。

また、クラスのドットがありませんheading

$('article.col').each(function(index, el) {
   $(el).find('.heading img').replaceWith('<newtag>somecode</newtag>');  
});

また

$('article.col').each(function() {
    $(this).find('.heading img').replaceWith('<newtag>somecode</newtag>');  
});
于 2013-04-30T00:14:37.867 に答える
0
(function ($) {
  $(document).ready(function() {
    $('article.col').each(function(el) {
       $(this).find('heading img').replaceWith('<newtag>somecode</newtag>');  
   });
  });
 }(jQuery));

ところで、jQuery ラッパーは、$ 記号を使用して他のライブラリとの競合を防ぐためのものです。

于 2013-04-30T00:07:07.020 に答える
0

構文にはいくつかの問題がありますが、

$(el.find('heading img').replaceWith('<newtag>somecode</newtag>')); 

する必要があります

$(this).find('heading img').replaceWith('<newtag>somecode</newtag>'); 

このフィドルを参照してください:

http://jsfiddle.net/5bEq7/1/

于 2013-04-30T00:13:10.183 に答える