简体   繁体   English

在鼠标悬停标题上更改文本颜色

[英]Change text color on mouseover caption

I'm trying to set up a page where there will be several titles in a row, with a descriptive paragraph below. 我正在尝试建立一个页面,其中连续有多个标题,下面是描述性段落。 When the user scrolls over the titles, the paragraph will change corresponding to the title they are hovering over. 当用户在标题上滚动时,段落将根据他们悬停的标题进行更改。 I would like each title to be a different color, and the font color of the paragraph should change to match the title's color. 我希望每个标题使用不同的颜色,并且段落的字体颜色应更改为与标题的颜色匹配。 The problem I'm having is getting the paragraph's color to change. 我遇到的问题是更改段落的颜色。

Here's my example: 这是我的示例:

<script type="text/javascript">
function onover(caption)
{document.getElementById('text').innerHTML=caption;}
{document.getElementById('text2').innerHTML=caption;}
</script>

<a onmouseover="onover('Paragraph 1')" style="color:green">Title 1</a>
<a onmouseover="onover('Paragraph 2')" style="color:red">Title 2</a>
</br>
<div id="text" style="color:green">Paragraph 1</div>
<div id="text2" style="color:red"></div>

Updated Since OP wanted to replace the text, 更新由于OP希望替换文本,

<script type="text/javascript">
       function onover(caption,color) {
          document.getElementById('text').innerHTML=caption;
          document.getElementById('text').style.color=color;
          document.getElementById('text2').style.color=color;
   } 
</script>

<a onmouseover="onover('Paragraph 1','green')" style="color:green" id='one'>Title 1</a>
<a onmouseover="onover('Paragraph 2','red')" style="color:red">Title 2</a>
</br>
<div id="text" style="color:green">Paragraph 1</div>

Demo 演示

Update 2 更新2

<script type="text/javascript">
function onover(caption, objId, obj)
{
   document.getElementById(objId).innerHTML=caption;
   document.getElementById(objId).style.color = obj.style.color;
}
</script>

<a onmouseover="onover('Paragraph 1', 'text', this)" style="color:green">Title 1</a>
<a onmouseover="onover('Paragraph 2', 'text2', this)" style="color:red">Title 2</a>
</br>
<div id="text" style="color:green">Paragraph 1</div>
<div id="text2" style="color:red"></div>
document.getElementById('text2').style.color = 'blue';

would change the color of text. 会改变文本的颜色。

on a separate note, {document.getElementById('text2').innerHTML=caption;} is not part of your onover function. 在单独的注释上, {document.getElementById('text2').innerHTML=caption;}不是onover函数的一部分。

I'd strongly recommend separating style, logic, and content. 我强烈建议您将样式,逻辑和内容分开。 It makes your code much easier to read maintain. 它使您的代码更易于阅读和维护。 Some recommendations: 一些建议:

  • All inline styling (eg ) should be replaced by stylesheets 所有内联样式(例如)都应替换为样式表
    • In this example doing so allows you to reuse the same styling for both the menu item and the paragraph 在此示例中,这样做允许您对菜单项和段落重复使用相同的样式
  • Make use of a third-party JavaScript library like jQuery to simplify handling cross-browser javascript implementation inconsistencies 利用jQuery之类的第三方JavaScript库简化跨浏览器javascript实现不一致的处理
  • Attach events progmatically rather than inline 以编程方式附加事件,而不是内联
  • Don't try to handle each case individually; 不要试图单独处理每种情况; abstract your code a little more. 多抽象一些代码。 There's no need to create multiple div's just to get the styling to match 无需创建多个div即可使样式匹配
  • Generally speaking, it is a good idea to place all script content at the end of the page, just before the closing body tag. 一般来说,将所有脚本内容放在页面的末尾,紧接在结束body标签之前是个好主意。 This will make your content visible while the page is processing the javascript and give the site the appearance of loading faster 这将使您的内容在页面处理javascript时可见,并使网站的加载速度更快

Here's an example of how to implement these suggestions: 这是如何实施这些建议的示例:

<style>
  .red { color: red; }
  .green { color: green; }
</style>
<div id="TitleBar">
  <a data-text="Paragraph 1" class="green">Title 1</a>
  <a data-text="Paragraph 2" class="red">Title 2</a>
</div>
<div id="text"></div>
<script src="http://code.jquery.com/jquery-git.js"></script>
<script>
  $('#TitleBar').on('hover', 'a', function (e) {
    var $evTarg = $(e.target),
        $dest = $('#text');
    $dest.html($evTarg.data('text')).attr('class', $evTarg.attr('class'));
  });
</script>

​ One last comment - if the "Paragraph x" text is more than a couple of words, consider storing the text in a named array instead of embedding it in the source tag. **最后一个注释-如果“ Paragraph x”文本超过几个单词,请考虑将文本存储在命名数组中,而不是将其嵌入到源标签中。

You'd better do that via CSS. 您最好通过CSS做到这一点。 No programing needed at all. 完全不需要编程。

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

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