简体   繁体   English

我的 jquery 代码有什么问题?

[英]What is wrong with my jquery code?

I have this div:我有这个 div:

<div class="transparency" data-title="<? echo $perf; ?>">&nbsp;</div>

An this is the jquery code which extracts the data-title value and replaces the &nbsp with it.这是 jquery 代码,它提取数据标题值并用它替换 &nbsp。

function performerNames(){

    var performername = $(".transparency").data('title'); 

    var divcontent = $(".transparency").innerHTML;

    if (divcontent == '&nbsp;'){

        $(".transparency").html(''+divcontent+''); 

    }

}

Ty very much!非常喜欢!

Use html() instead of innerHTML ::使用html()代替innerHTML ::

var divcontent = $(".transparency").html();

and you want to replace by data, so:并且你想用数据替换,所以:

$(".transparency").html(''+performername+'');

instead of:代替:

$(".transparency").html(''+divcontent+''); 

UPDATE:更新:

whole code:整个代码:

function performerNames(){    
    var performername = $(".transparency").data('title');     
    var divcontent = $(".transparency").html();    
    if (divcontent == '&nbsp;'){    
        $(".transparency").html(''+performername+'');     
    }
}

The innerHTML should not ever be &nbsp; innerHTML 不应该是&nbsp; unless you double entity encoded your non-brekaing space.除非您对非中断空间进行双重实体编码。 Thus the body of your if block does not execute.因此,您的 if 块的主体不会执行。

EDIT: This, as suggested by others, works for me:编辑:正如其他人所建议的,这对我有用:

function performerNames()
{
    var $transparency = $( '.transparency' ),
        performername = $transparency.data( 'title' );

    if( $transparency.html() == '&nbsp;' )
    {
        $transparency.html( performername );
    }
}

Replace $(".transparency").innerHTML;替换$(".transparency").innerHTML; with $(".transparency").html();$(".transparency").html();

You're just setting the html content to be the same as it is:您只是将 html 内容设置为与其相同:

var divcontent = $(".trasparency").html();
$(".transparency").html(divcontent);

Which should be应该是哪个

$(".transparency").html(performername);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM