如何更改应用于多个元素中的跨度的类?

时间:2018-06-27 16:11:12

标签: javascript jquery html css

我正在开发一个应用程序,我有几个跨度。例如:

<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 1 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 2 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 3 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 4 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 5 </a>

我想通过jQuery执行以下操作。

1)我想在单击时更改一个跨度的类而又不影响另一跨度,这样就保留了这样的内容:

<a id="boton"> <span #span class = "glyphicon glyphicon-minus"> </ span> item 1 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 2 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 3 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 4 </a>
<a id="boton"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 5 </a>

2)我具有以下实现的代码,但它意味着用不同的id重复执行几次该代码,但我不想这样做,我想创建一个执行此操作的通用函数。

$ ('# button'). click (function () {
 $ ('# span'). toggleClass ('glyphicon-minus');
});

你能帮我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

您可能会获得clickables的父项,然后进行相应的设置。您可以检查

Sample Code Here

Gs.getR()[1] = .1;
$ ('a'). click (function (event) {

var target = $( event.target );
  if ( target.is( "span" ) ) {
    target.toggleClass('glyphicon-minus');
  }
})
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}

在此代码中,如同在HTML代码中一样为锚标记处理事件,您没有唯一的ID,或者您可以在HTML本身中编写click事件。

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<a id="button" herf="#"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 1 </a>
<br />
<a id="button"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 2 </a>
<br />
<a id="button"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 3 </a>
<br />
<a id="button"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 4 </a>
<br />
<a id="button"> <span #span class = "glyphicon glyphicon-asterisk"> </ span> item 5 </a>

希望它会对您有所帮助。