简体   繁体   English

更改 jQuery 选取框文本和 css/样式

[英]Changing jQuery marquee text and css/style

I have div我有div

<div id="rotatingText" class="marquee">
</div>

and marquee on that div is initialized on window load并在窗口加载时初始化该 div 上的选取框

jQuery('.marquee').marquee({
        duration: 3000,
        gap: 50,
        delayBeforeStart: 0,
        direction: 'left',
        duplicated: false
    });

Now i need to change text and css of that div on ajax aftersuccess.现在我需要在成功后在ajax上更改该div的文本和css。 I get new text and css properties in that ajax response.我在该 ajax 响应中获得了新的文本和 css 属性。 With that text and css I invoke following:使用该文本和 css 我调用以下内容:

function setRotatingTextProperties( value, css, objectId )
{
    var object = document.getElementById( objectId );
    object.innerHTML = value;
    jQuery ( object ).attr( 'style', css );
}

After that marquee stops working.在那之后选框停止工作。 I think that jQuery marquee sets some style in that div and i override it with:我认为 jQuery 选框在那个 div 中设置了一些样式,我用以下方法覆盖它:

jQuery ( object ).attr( 'style', css );

if I just add my css to existing with如果我只是将我的 css 添加到现有的

var currentCss = jQuery ( object ).attr( 'style');
jQuery ( object ).attr( 'style', currentCss + css );

with multiple changes I will end up with huge amount of unusable css.通过多次更改,我最终会得到大量无法使用的 css。 And that css will change regularly ( every fiew seconds )... Any advice and idea will be much appreciated.并且 css 会定期更改(每隔几秒)...任何建议和想法将不胜感激。 Thx in advance.提前谢谢。

Actually I think that your problem is not in your css and:其实我认为你的问题不在你的 css 中,并且:

jQuery ( object ).attr( 'style', css );

In my experience problem with jQuery marquee is in changing of text.根据我的经验,jQuery 选框的问题在于更改文本。 so your problem is:所以你的问题是:

object.innerHTML = value;

Best solution ( If you can change html of that page ) is to add span into it:最佳解决方案(如果您可以更改该页面的 html )是将 span 添加到其中:

<div id="rotatingText" class="marquee">
        <span id="rotatingTextSpan"></span>
    </div>

And change span text, and style of the div.并更改跨度文本和 div 的样式。 something like:就像是:

function setRotatingTextProperties( value, css )
{
        // change span text value
        $( '#predefineImageRotatingTextSpan' ).text( value );
        // change css value on div
        $( '#predefineImageRotatingText' ).attr( 'style', css );
}

If you can't change HTML of that page for any reason you can always insert span value into it on window.load or document.ready, but BEFORE you initialize marquee with something like如果您因任何原因无法更改该页面的HTML ,您始终可以在 window.load 或 document.ready 上将 span 值插入其中,但使用类似的内容初始化选取框之前

 $( '#predefineImageRotatingTextSpan' ).html( '<span id="predefineImageRotatingTextSpan"></span>' );

and then use above function.然后使用上面的功能。

EDIT:编辑:

If you have some predefined text in your div at the beginning just copy its value into that div:如果开始时您的 div 中有一些预定义的文本,只需将其值复制到该 div 中:

var $object = $( '#predefineImageRotatingTextSpan' )
var initialText = $object.text()
$object.html( '<span id="predefineImageRotatingTextSpan"> ' + initialText  + '</span>' );

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

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