如果在页面上存在如何将新的类和属性添加到div [英] How to add new class and attributes to div if it exists on the page

查看:90
本文介绍了如果在页面上存在如何将新的类和属性添加到div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的母版页上需要JavaScript代码,它试图查找是否存在div。如果是这样,它应该添加一个新的类,并添加一个新的ID属性。

I need JavaScript code on my master page which tries to find if a div exists. If so, it should add a new class and also add a new id attribute.

例如,如果页面有这个div:

For example if the page has this div:

<div id="Top" class="toplink">abc..</div>

然后JavaScript代码应该完全像这样:

Then JavaScript code should make it exactly like this:

<div id="Top" class="toplink adin" id-adi="114">abc..</div>

div内的代码应该保持不变。

The code inside the div should remain the same.

推荐答案

感谢大家
以下代码正在为我工​​作

Thanks all of you following code is working for me

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
    if ($('.toplink').length > 0) {
        $('.toplink').addClass('adin').attr('data-aid', '114');
    }
});
</script>

回答iamruss

新的stackoverflow,我问几乎相同的问题在如果在页面上存在,则将新的类和属性添加到div

As i am new to stackoverflow, i ask almost same question at Add new class and attributes to div if it exists on the page

这篇关于如果在页面上存在如何将新的类和属性添加到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆