简体   繁体   English

使用JQuery和滚动代码更改文本颜色

[英]Changing text colour with JQuery alongside scrolling code

I have a page in which the user can click on a date in the sidebar, which automatically adds that date to a form, and also scrolls down to the form using AnimateScroll.js . 我有一个页面,用户可以在其中单击侧边栏中的日期,该日期将自动将该日期添加到表单,并使用AnimateScroll.js向下滚动到表单。 This works fine. 这很好。

I also have a button in the main part of the page that, when clicked, again uses AnimateScroll to scroll up to the top of the list of dates so the user can click their desired one. 我在页面的主要部分也有一个按钮,单击该按钮后,再次使用AnimateScroll向上滚动到日期列表的顶部,以便用户可以单击所需的日期。 This too works fine, but what I'd like to do is also highlight the paragraph above the dates list in red so the user clearly sees what they're supposed to do. 这也很好用,但是我想做的是用红色突出显示日期列表上方的段落,以便用户清楚地看到他们应该做什么。 AnimateScroll is called like this: AnimateScroll的调用方式如下:

 <a class="button" onclick="$('#dates').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});">Book Now</a>

I'm sure I should be able to add something else to the code executed onclick, probably jQuery, but I don't know what that is. 我确定我应该能够向onclick执行的代码中添加其他内容,可能是jQuery,但是我不知道那是什么。 Can you help? 你能帮我吗?

There are several ways to do this. 有几种方法可以做到这一点。 One quick way is: 一种快速的方法是:

  • Just embed this line of code there 只需将这行代码嵌入其中

    $('#dates').css('color','red'); $('#dates')。css('color','red');

(In case the container where the date appears has the id 'dates', otherwise check the class or id of the container and change the selector accordingly). (如果出现日期的容器的ID为“ dates”,则请检查容器的类或ID并相应地更改选择器)。

Give an id to the paragraph Say id="Paragraph" <p id="Paragraph"> Please chhose the date</p> 在段落中输入一个ID说“ id =“ Paragraph” <p id="Paragraph"> Please chhose the date</p>

Now, Add a line in your onclick 现在,在onclick中添加一行

<a class="button" onclick="$('#Paragraph').css('color','red'); $('#dates').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});">Book Now</a>

This will acheive what you want..!! 这将实现您想要的.. !!

for maintanance it would be MUCH nicer to write the javascript in an external javascript file. 为了维护,将 javascript编写在外部javascript文件中要好得多 also for many buttons you don't need to write the onclick every single time you can just have it at one place 也为许多按钮,你不需要写的onclick每一次你可以把它在一个地方

<a class="button" href="javascript:;">Book Now</a>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('.button').on('click',function(){
            $('#dates').css({
                'font-size' : '10px',
                 width : '30px',
                 height : '10px'
             });
            $('#dates').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});
        });
    });
</script>

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

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