0

変数の内容を別の変数に代入しようとしています。どうすればいいですか?これまでのところ、私はこれを試しました:

var className = $($(this).attr("class"););

$('.content').html(className)

編集:

「var className = $($(this).attr("class"););」の終了値を想定してください。ポイント1です

ポイント 1 には以下が含まれます。

編集2:(完全なコードが含まれています)

    <div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>

    <script type="text/javascript">
    var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';
    var className = "Broken";
    $('.content [class] [class]').click(function () {$(this).fadeTo(250, 0.25, function () {
    className = $(this).attr("class");
    $('.content').html(className)

    $(this).fadeTo(250, 1.00);

    });}
    );

    </script> 


var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';

"$('.content').html(className)" < このクラス名には point1 に文字列が含まれている必要があります)

4

4 に答える 4

1

私が明確に理解したと仮定してOK。いくつかの要素のクラス名と同じで、その要素をクリックすると、いくつかの変数に格納されたいくつかのhtmlがあります。その変数から html を取得し、コンテンツとして classname を持つ別の要素に割り当てたいとします.content

$('.someClass').on('click',function(){
    var className = this.className; // Get the classname whic is a variable
   var content = window[className];
   if(content)
    $('.content').html(content); //Assuming variable is defined in the window Scope. 
});

これを分割する:-

window[className] <-- to get the variable defined in the window scope.

Update:-

Try this based on your code update. Since you are replacing the content and the click to happen again you should go with delegated event handler using .on(), use .live for older version of jquery.

   var className = "Broken";
   $(document).on('click', '.content [class] [class]', function () {
       $(this).fadeTo(250, 0.25, function () {
           className = this.className;
           $('.content').html(window[className]);
           $(this).fadeTo(250, 1.00);

       });
   });

Demo

于 2013-05-21T03:41:04.593 に答える
0

編集

これがあなたがやろうとしていることのようです

var className = $('.' + $(this).attr("class")).html();
$('.content').html(className);
于 2013-05-21T03:30:09.790 に答える
0

コードに問題がないように見える以外に、構文上のエラーがあります。

あなたがしたいことは、要素のクラスプロパティの値を要素に表示することであると仮定しthisます.content

var className = $(this).attr("class");
$('.content').html(className)

デモ:フィドル

更新:がオブジェクトに関連付けられたプロパティの名前である
と仮定すると、値をフェッチするために使用できます。 変数がグローバルコンテキストで作成された場合、値を取得するために使用できますclassNamexx[className ]
window[className]

var point1 = '<div class="content"><h1>testing hello world</h1><div class="position"> <div class="point1"> test<br> awewa<br> TEST <h1> test </h1> </div></div></div>';
jQuery(function($){
    var className = "Broken";
    $('.content').on('click', '[class] [class]', function () {
        $(this).fadeTo(250, 0.25, function () {
            className = $(this).attr("class");
            $('.content').html(window[className])
        });
    });
});

デモ:フィドル

于 2013-05-21T03:35:58.047 に答える
0

'this' が要素を表していると仮定すると ($.each() コールバック内にいる場合のように、$(this).attr('class') はその要素に割り当てられたすべての CSS クラスを返します。これは、 'class=""' に表示される値のみに影響するすべての CSS ルール。外側の $() は、質問の最初の代入からの戻り値を非表示にします。2 行目は、innerHTML を設定します最初の行が返した値にクラス "content" を持つ要素。

于 2013-05-21T03:34:21.730 に答える