使用 jquery,我们可以通过响应鼠标单击事件来使网页具有交互性。 jquery 包括事件、效果、动画、ajax、json 解析、跨浏览器支持和可扩展性等功能。它作为单个 javascript 文件分发,该文件定义了所有接口,如 dom、事件和 ajax 函数。
jquery 由两个函数组成,一个是静态实用函数,另一个是对象方法。
我们可以使用 jquery 中的 attr() 函数来更改元素 id 的值。您还可以使用 prop() 函数。
让我们详细讨论一下 jquery 的这两个方法。
方法 1:使用 jquery attr( ) 方法这是更改元素id的方法之一。可以使用 attr() 方法设置或返回所选元素的属性和值。如果 jquery attr() 返回该属性,则它返回匹配的 html 元素的第一个值。如果 jquery attr() 设置属性值,那么它会为一组匹配的 html 元素设置一个或多个属性或对值。
语法以下是 attr() 方法的语法 -
要返回属性的值,如下所示 -
$(selector).attr(attribute)
用于设置值和属性
$(selector).attr(attribute, value)
使用函数设置值和属性,如下所示 -
$(selector).attr(attribute, function(index, currentvalue))
为了设置多个值和属性,使用以下内容 -
$(selector).attr({attribute:value, attribute:value,…})
参数属性 - 指定属性名称。
value - 属性的指定值。
function(index,currentvalue) - 指定一个返回要设置的属性值的函数。这里,索引接收集合内元素中索引的位置。 currentvalue 接收所选元素的当前属性值。
示例在这些示例中,我们将使用 jquery 的 attr() 方法更改元素 id。
<html><head> <script src =https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js></script> <style> #mycolor {color: white; background: black; padding:30px; height: 90px; width: 400px; } #newcolor {background: pink; width: 650px; color: white; padding:30px; height: 90px; } </style></head><body> <div id=mycolor><p style=text-align:center;>changing the element id</p> </div><br> <button onclick = myfuntion()> click here </button> <script> function myfuntion() { $(div).attr('id', 'newcolor'); } </script> </center></body></html>
正如我们在示例中看到的,这里我们使用了 attr() 方法,该方法用于更改 html 元素的元素 id,它是一个 jquery 属性方法。我们需要导入 jquery 库来执行 jquery 相关的功能。
可以使用 attr() 方法设置或返回所选元素的属性和值。如果使用 jquery attr() 返回属性,则返回第一个匹配元素的值。这些属性方法是改变元素的元素id。
在我们点击按钮之前,背景颜色显示为黑色,一旦我们点击按钮,背景颜色就会变成粉红色。
方法 2:使用 prop() 方法我们可以使用 prop() 方法更改 html 元素的元素 id。它用于设置或返回所选 html 元素的属性和值。当使用 jquery prop() 返回属性值时,将返回第一个匹配元素的值。如果使用 jquery prop() 方法来设置属性的值,则为一组匹配元素的值对的一个或多个属性,这些元素是集合。
以下是 prop() 方法的语法 -
用于返回属性的值,如下 -
$(selector).prop(property)
用于设置属性和值
$(selector).prop(property, value)
使用函数设置属性和值,如下所示 -
$(selector).prop(property, function(index, currentvalue))
设置多个属性和值如下 -
$(selector).prop(property:value, property:value,…))
参数属性 - 指定属性名称
value – 指定属性值
function(index,currentvalue) – 指定返回要设置的属性值的函数。这里,索引接收集合中 html 元素的索引位置。 currentvalue 接收当前所选元素的属性值。
示例让我们再举一个例子,看看如何使用 prop( ) 方法更改元素 id。
<html><head> <script src =https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js></script> <style> #mycolor {color: white; background: black; padding:30px; height: 90px; width: 550px; } #newcolor {background: green; width: 550px; color: white; padding:30px; height: 90px; } </style></head><body> <div id=mycolor><p style=text-align:center;>changing the element id</p> </div><br> <button onclick = myfuntion()> click here </button> <script> function myfuntion() { $(div).prop('id', 'newcolor'); } </script> </center></body></html>
正如我们在示例中看到的,这里我们使用 prop() 方法来更改 html 元素的元素 id,这是一个 jquery 属性方法。
在我们点击按钮之前,背景颜色显示为黑色,一旦我们点击按钮,背景颜色就会变成绿色。
结论在本文中,我们通过示例演示了如何更改元素 id。我们在这里看到了两个不同的示例,在第一个示例中,我们使用 attr() 方法和 onclick 事件在单击按钮后更改元素 id。在第二个示例中,我们使用 prop() 方法和 onclick 事件在单击按钮后更改元素 id。
以上就是如何使用 jquery 更改元素 id?的详细内容。