2016-11-30 9 views
0

私はJavascriptのゴールドスターメダルではありませんので、私にご負担ください。jqueryで読み込んだURLを別のURLに置き換えます。

私たちは、javascript(レートカードと呼ばれます)経由で外部から読み込まれているサイトに追加していますが、そのアイテムがisntのために使用されているリンクは正しいので、少しjavascript/jqueryそれを上書きしようとしています。

プラグインが読み込まれると、定義済みのdivにlocation.hrefが追加されます。私はJavaScriptを使ってこれを上書きしようとしています。

これは、だから私は、最初のクリックアクションをバインド解除し、新しいものを追加しようとしています、まだ何も起こりません

<div class="wdgt_widget" style="border:1px solid #1d8be0 !important; border-radius:8px !important; padding:11px !important; width:300px !important; height:64px !important; cursor:pointer !important; box-sizing: border-box !important;" onclick="location.href='https://ratecard.io/staffing-ms'"></div> 

を生成されているdiv要素です。以下のスクリプトを参照してください。

 <script> 
      jQuery(document).ready(function ($) { 
       setTimeout(function() { 
        $('#wdgt_widget').unbind(); 
        $('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/"); 
       }, 5000); 
      }); 
     </script> 

私はコードがロードされ、divが既に生成されていることを確認したいので、5秒タイマーを追加しました。

悲しいことに何も起こりません。単にonclickで置き換えを使用する可能性はありますか?または私はここで何か悪いことをしましたか?

+0

'#wdgt_widget'の代わりに' .wdgt_widget'を使うべきです。私は考えます。あなたの 'div'はidを持っていないので。 –

+0

であり、属性名は 'クリック 'ではない' onclick'です – Archer

+0

'unbind()'はインラインスクリプトをアンバインドしません。あなたの投稿コードにシンタクスエラーがあります... –

答えて

1

問題: 1)あなたはIDによって選択するために使用されるセレクタで#使用しているこの$('#wdgt_widget')を。 2)あなたが最後に'が欠落している:Replcaeは上記のこの$('.wdgt_widget')


問題で構文を述べた) 1:しかし、あなたはあなたのdivでクラスを持っているので、.

ソリューションを使用しますまた、clicklocationの間には,と表示されます。

$('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/"); // , and ' missing.

溶液: 2)変化.attr('click' 'location.href="https://ratecard.io/staffing-ms/review/") .attr('click','location.href="https://ratecard.io/staffing-ms/review/"')に注意私は端と',を添加しました。

+1

まさに私が探していたものです。私はこれを私のクラスとIDのミックスアップを既に投稿していたときに気付いた...しかし、最後のコマンドと引用が私のためにやった。恐ろしい – Dorvalla

+0

@Dorvalla私は助けることができてうれしい。 –

0

#wdgt_widgetの代わりに.wdgt_widgetを使用する必要があります。あなたのdivにはIDがないので

<div class="wdgt_widget" style="border:1px solid #1d8be0 !important; border-radius:8px !important; padding:11px !important; width:300px !important; height:64px !important; cursor:pointer !important; box-sizing: border-box !important;" onclick="location.href='https://ratecard.io/staffing-ms'"></div> 

<script> 
      jQuery(document).ready(function ($) { 
       setTimeout(function() { 
        $('.wdgt_widget').unbind(); 
        $('.wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/"); 
       }, 5000); 
      }); 
     </script> 
0
$(document).ready(function() { 
    setTimeout(function() { 
    $('.wdgt_widget').unbind().attr('onclick' 'location.href="https://ratecard.io/staffing-ms/review/"); 
    }, 5000); 
}); 
1

あなたはこれを行う必要があります。

$(document).ready(function() { 
    setTimeout(function() { 
     $('.wdgt_widget') 
      .unbind() 
      .attr('onclick', 'location.href="https://ratecard.io/staffing-ms/review/"'); 
    }, 5000); 
}); 
関連する問題