简体   繁体   English

如何获取点击链接的 id,然后在 CSS 中设置样式

[英]How do I get the id of a clicked link and then style it in CSS

How do I style a clicked link with CSS in JavaScript?如何在 JavaScript 中使用 CSS 设置点击链接的样式?

Here is my link:这是我的链接:

<li><id="106" onClick="configurator(this)" href="#">10.6 &micro;</a><li>

I am trying to run a function called configurator and return the ID of the this link so I can set the CSS.我正在尝试运行一个名为configurator的函数并返回此链接的 ID,以便我可以设置 CSS。

Help!帮助! I am a JS newbie!我是JS新手!

Here is my function:这是我的功能:

function configurator(clicked) {
 
 document.getElementById(clicked);
 alert(clicked.name);  
}

You can style an element like this:您可以像这样设置元素的样式:

element.style.color = 'white';
element.style.backgroundColor = 'red';

To get the element which was clicked you could use the event:要获取被点击的元素,您可以使用该事件:

function doSomething(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
}

Example function from quirksmode.org .来自quirksmode.org 的示例函数。

EDIT : Other thing: It's bad practice to add onclick properties directly to the html.编辑:其他事情:将 onclick 属性直接添加到 html 是不好的做法。 You could use unobtrusive java script .您可以使用不显眼java脚本

EDIT 2 : A jQuery example.编辑 2 :一个 jQuery 示例。

$(function() // Alias for $(document).ready()
{
    $('li a').click(function()
    {
        $(this).css({
            color: 'red'
        });
    });
});

If your onClick function is called as "configurator(this);", then you passed it the element itself, not the ID of the element.如果您的 onClick 函数被称为“configurator(this);”,那么您将元素本身传递给它,而不是元素的 ID。

function configurator(clicked) { 

// document.getElementById(clicked); // doesn't work because "clicked" is an element, not an ID
 clicked.style.fontWeight = "bold";
 // OR
 clicked.className = "boldClass";
 alert(clicked.name);   
} 

EDIT:编辑:

Also just noticed that you are missing the "a" tag.还刚刚注意到您缺少“a”标签。 It should be:它应该是:

<li><a id="106" onClick="configurator(this)" href="#">10.6 &micro;</a><li> 

There are a few problems with the above example, I've tried to clean it up as best I can.上面的例子有一些问题,我已经尽力清理它。 Here's code that I tested, that works by itself.这是我测试过的代码,它本身就可以工作。 It's not pretty, but it works:它不漂亮,但它有效:

<li><a href="#" id="106" onClick="configurator(this)">10.6 &micro;</a></li>

<script type="text/javascript">
    function configurator(clicked) {
         alert(clicked.id);
    }
</script>

You can use Element.addEventListener() to achieve this.您可以使用Element.addEventListener()来实现这一点。

This is completely untested but it should give you some direction.这是完全未经测试的,但它应该给你一些方向。 Passing the whole css object came to me on the fly, you could alternatively omit this and hardcode the styles you are trying to change (but why would you wanna do that =P).传递整个 css 对象是即时传给我的,您也可以省略它并对您尝试更改的样式进行硬编码(但您为什么要这样做 =P)。

// Function to change the content of t2
function modifyStyle(styles) {
   //save element
   var that = this;
   for each (property in styles){
      that.style.property = styles[property];
   }  
}

// Function to add event listener anchor
function load() { 
  var el = document.getElementById("myAnchor"),
  cssObj = {
      background-color: 'black',
      font-size: 1.2em
  }
  //set the function call to an anonymous function that passes your css object
  el.addEventListener("click", 
                      function(){modifyStyle(cssObj)}, 
                      false); 
} 
<li><a id="106" onClick="configurator(this.id)" href="#">10.6 &micro;</a><li> 

function configurator(clicked) 
{  

 var elementClicked = document.getElementById(clicked); 
 //Set the styles using the ID element.clicked.style......
 alert(clicked);   
} 

Use document.getElementById(clicked);使用document.getElementById(clicked); to get the sent ID and then you can set the styles to this.获取发送的 ID,然后您可以将样式设置为此。 You missed out the a tag, and you was passing the element name not ID.您错过了a标签,并且您传递的是元素名称而不是 ID。

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

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