如果我使用“getElementById”要更改单个对象的 CSS 样式,如何更改继承该样式的所有元素?

发布于 2024-09-14 06:00:18 字数 2689 浏览 12 评论 0原文

我见过的所有示例都使用“getElementById”来获取单个元素,然后更改该单个元素的样式。

我处于需要使用样式修改页面上所有匹配元素的情况。我需要更改字体大小、高度和宽度。我该如何做到这一点,jQuery 是必需的还是可选的?我问的原因是因为这个网站不使用 jQuery,我不想仅仅为了完成这一件事而下载整个库。

更新 举个例子,假设我在页面上有几个具有这种样式的元素:

.outerContact
{
    border: 0px;
    margin: 7px;    
    float: left;
   padding: 0px; 
 background: url(/TLSADMIN/UserControls/contactsgrid/trans-shadow.png) no-repeat bottom right; /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */        
}
.contactLarge{
    height: 163px;
    width: 250px;
    border: 1px solid #ccc; 
    border-top: 1px solid #ddd;
    font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    font-size:small;
    margin: -5px 5px 5px -5px; /* Offset the image by certain pixels to reveal the shadow, as the shadows are 6 pixels wide, offset it by that amount to get a perfect shadow */

  /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */     
     background-image: url('/TLSADMIN/UserControls/contactsgrid/OutlookContactGradient.png') ;
     background-repeat:repeat-x;
      background-position: bottom;
padding: 0px; /* Increasing this gives a white border around the image */ 
background-color: #f2f6f9; /* Background color of the border created by the padding */
border: 1px solid #cecece; /* A 1 pixel greyish border is applied to the white border created by the padding */ 
display: block; /* IE won't do well without this */ 
position: relative; /* Make the shadow's position relative to its image */ 

}

然后假设我有一个 JavaScript 函数,它将根据滑块按比例调整上面元素的大小。我正在使用的滑块可从此处的第 3 方获得: http://aspnetajax.componentart.com/control-specific/ slider/features/custom_Scrollbar/WebForm1.aspx

然后,该滑块将传递一个数字,我将使用该数字来确定放大/缩小的程度:

function ResizeWindowAccordingToScrollBar(PercentChange)
{
 //Locate elements
 //Resize fonts, borders, all styles to zoom in/ zoom out.

}

您建议我如何处理“PercentChange”值?我可以使用 switch 语句替换每个匹配元素的 CSS 样式,但这可能不如其他选项那么顺利。

UPDATE2

另外,如果有人想查看我的代码,这里有一个独立的示例: http://www.perfmon.com/download/StackOverflow_ContactsGrid.zip

如果您下载ComponentArt 控件,请随意取消注释滚动条代码。

我的目标是直接模拟 Outlook 2007 中提供的缩放功能

alt text

All the examples I've seen use "getElementById" to get the single element, and then change that style for that singular element.

I'm in the situation where I need to modify all the matching elements on the page with a style. I need to change the font size, height, and width. How do I do this, and is jQuery required or optional? The reason I ask is because this site doesn't use jQuery and I'd rather not download the entire library just to accomplish this one thing.

Update
As an example, suppose I have several elements on the page with this style:

.outerContact
{
    border: 0px;
    margin: 7px;    
    float: left;
   padding: 0px; 
 background: url(/TLSADMIN/UserControls/contactsgrid/trans-shadow.png) no-repeat bottom right; /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */        
}
.contactLarge{
    height: 163px;
    width: 250px;
    border: 1px solid #ccc; 
    border-top: 1px solid #ddd;
    font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    font-size:small;
    margin: -5px 5px 5px -5px; /* Offset the image by certain pixels to reveal the shadow, as the shadows are 6 pixels wide, offset it by that amount to get a perfect shadow */

  /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */     
     background-image: url('/TLSADMIN/UserControls/contactsgrid/OutlookContactGradient.png') ;
     background-repeat:repeat-x;
      background-position: bottom;
padding: 0px; /* Increasing this gives a white border around the image */ 
background-color: #f2f6f9; /* Background color of the border created by the padding */
border: 1px solid #cecece; /* A 1 pixel greyish border is applied to the white border created by the padding */ 
display: block; /* IE won't do well without this */ 
position: relative; /* Make the shadow's position relative to its image */ 

}

And then assume I have a JavaScript function that will proportionally resize the elements above according to a slider bar. The slider bar I'm using is available from a 3rd party here:
http://aspnetajax.componentart.com/control-specific/slider/features/custom_Scrollbar/WebForm1.aspx

That slider will then pass a number that I'll use to determine how much to zoom in/out:

function ResizeWindowAccordingToScrollBar(PercentChange)
{
 //Locate elements
 //Resize fonts, borders, all styles to zoom in/ zoom out.

}

How do you recommend I handle the "PercentChange" value? I may swap out the CSS style for each matching element using a switch statment, but that may not be as smooth as other options could be.

UPDATE2

Also, if someone wants to look at my code, a self contained sample is here:
http://www.perfmon.com/download/StackOverflow_ContactsGrid.zip

If you download the ComponentArt Controls, feel free to uncomment the scrollbar code.

My goal is to directly emulate the zoom feature available in Outlook 2007

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难理解 2024-09-21 06:00:18

如果你谷歌 getelementbyclassname 可能对你有帮助。与 getelementbyid 类似的想法,但通过类名获取它们,您可以根据需要修改它们的样式。

If you google getelementbyclassname that might help you. Similar idea to getelementbyid but gets them by classname you can them modify their styles as you see fit.

埋葬我深情 2024-09-21 06:00:18

假设您有一些像这样的 HTML...

<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p>
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="iconic" alt="a petty lady" />
</div>

您需要其中一些元素可缩放/调整大小。首先,您需要使用 元素标识符< /a>; id 属性或 class 属性。

两者之间的区别是 id 属性必须是唯一的;由于我们需要将多个项目标识为可缩放,因此我们将使用 class 属性。我们将使用适当的、不言而喻的值,例如 zoomable

<div>
    <h2>Example</h2>
    <p>This is an example</p>
<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p class="zoomable">
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="zoomable iconic" alt="a petty lady" />
</div>
</div>

请注意, 元素已经具有 class 属性,但我们可以根据 w3c 推荐

该属性将一个类名或一组类名分配给一个元素。可以为任意数量的元素指定相同的类名称或名称。多个类名必须用空格字符分隔。

所以我们已经弄清楚了标记。现在介绍 JavaScript。

完成此操作后,您可以使用 document.getElementsByClassName(...) 函数来获取对所有这些 zoomable 元素的引用:

var zoomables = document.getElementByClassName('zoomable');
for(var i=0; i<zoomables.length; i++) {
    zoomables[i].style.height = '100px';
    zoomables[i].style.width = '100px';
    zoomables[i].style.fontSize = '20px';
}

...但不支持在 Internet Explorer 中,因此您必须使用对象检测来确定是否需要改为定义它:

if(!document.getElementsByClassName) {
    document.getElementsByClassName = function(className) { // this is the simplest, plainest, lowest-invested-time-and-effort
                                                            // version of getElementsByClassName  one can write...
        // you should probably sanitize that className input parameter a bit...
        var allElements = document.getElementsByTagName('*');
        for(var i=0; i<allElements.length; i++) {
            if(/\bclassName\b/.test(allElements[i].className)) {
                allElements[i].style.border = "2px solid red";
            }
        }
    }
}

这并不能完全解决您的问题,但它应该让您走上正确的道路。

So let's say you have some HTML like this...

<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p>
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="iconic" alt="a petty lady" />
</div>

You need some of these elements to be zoomable/re-sizable. First, you need to identify those elements using element identifiers; either the id attribute or the class attribute.

The difference between the two is an id attribute has to be unique; since we need several items to be identified as zoomable, we'll use a class attribute instead. We'll use an appropriate, self-evident value, like zoomable.

<div>
    <h2>Example</h2>
    <p>This is an example</p>
<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p class="zoomable">
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="zoomable iconic" alt="a petty lady" />
</div>
</div>

Note that the <img> element already had a class attribute, but we can assign that element to multiple classes, according the to w3c recommendation:

This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.

So we've figured out the markup. Now for the JavaScript.

Once we've done that, you can use the document.getElementsByClassName(...) function to get references to all those zoomable elements:

var zoomables = document.getElementByClassName('zoomable');
for(var i=0; i<zoomables.length; i++) {
    zoomables[i].style.height = '100px';
    zoomables[i].style.width = '100px';
    zoomables[i].style.fontSize = '20px';
}

... but it's not supported in Internet Explorer, so you'll have to use Object Detection to determine if you need to define it instead:

if(!document.getElementsByClassName) {
    document.getElementsByClassName = function(className) { // this is the simplest, plainest, lowest-invested-time-and-effort
                                                            // version of getElementsByClassName  one can write...
        // you should probably sanitize that className input parameter a bit...
        var allElements = document.getElementsByTagName('*');
        for(var i=0; i<allElements.length; i++) {
            if(/\bclassName\b/.test(allElements[i].className)) {
                allElements[i].style.border = "2px solid red";
            }
        }
    }
}

This doesn't completely solve your problem, but it should set you on the right path.

故事↓在人 2024-09-21 06:00:18

我的建议是在 CSS 文件中创建您需要的类并使用 jQuery 更改它。

.class1{background-color:#ff0000;}
.class2{background-color:#0000ff;}

在 jQuery 中:

$("#element").click(function(){
    $(".class1").removeClass("class1").addClass("class2");
});

这样,您不需要做任何“奇怪”和复杂的事情来获得您需要的结果。

My recommendation would be to create the class that you need in your CSS file and change it with jQuery.

.class1{background-color:#ff0000;}
.class2{background-color:#0000ff;}

And in jQuery :

$("#element").click(function(){
    $(".class1").removeClass("class1").addClass("class2");
});

This way, you do not need to do anything "weird" and complicated to get the result that you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文