简体   繁体   English

如果页面上加载了某个 DIV,则使用 JavaScript 更改元素的显示设置

[英]Using JavaScript to change display setting of an element if a certain DIV is loaded on page

I am very new to this and I'm having challenges with the way I type out my code.我对此很陌生,我在输入代码的方式上遇到了挑战。

Currently on my page there is the following button:目前在我的页面上有以下按钮:

<a ng-if="vm.cart.canRequestQuote" href="/Rfq" class="btn primary btn-request-quote tst_cartPage_submitQuote" ng-bind="vm.cart.isSalesperson ? 'Create a Quote' : 'Request a Quote'" ng-disabled="vm.cart.isAwaitingApproval">Request a Quote</a>

I am trying to hide/show this button (using JavaScript) based on whether or not the following is loaded on the page:我试图根据页面上是否加载以下内容来隐藏/显示此按钮(使用 JavaScript):

<div ng-if="product.quoteRequired" class="quote-required">

Please help.请帮忙。 I've tried a number of different scripts but I think because the button is a child of the div.tst_cartPage_buttons that I'm not typing the element out properly in JavaScript我尝试了很多不同的脚本,但我认为因为按钮是div.tst_cartPage_buttons的子元素,所以我没有在 JavaScript 中正确输入元素

Thank you for any guidance you can give.感谢您提供的任何指导。

you can use a jquery framework ( https://jquery.com/ ) and type:您可以使用 jquery 框架 ( https://jquery.com/ ) 并输入:

if ($('#idOfElementToSearch')!=null) $('#elementToHide').hide()

the $ function is useful for easily finding the elements because it uses the css selectors $ 函数对于轻松查找元素很有用,因为它使用了 css 选择器

$('.btn .primary .btn-request-quote .tst_cartPage_submitQuote').hide()

hide all element that have this 4 class隐藏所有具有这 4 类的元素

or if you don't want use a framework you can use Document.getElementById() or getelementbyclass instead of $ function, but is more harder或者,如果您不想使用框架,您可以使用 Document.getElementById() 或 getelementbyclass 而不是 $ 函数,但更难

Maybe you should try hidding the button with the same condition being used on your second element.也许您应该尝试隐藏第二个元素上使用的相同条件的按钮。

Something like:就像是:

<a ng-if="product.quoteRequired" href="/Rfq" class="btn primary btn-request-quote tst_cartPage_submitQuote" ng-bind="vm.cart.isSalesperson ? 'Create a Quote' : 'Request a Quote'" ng-disabled="vm.cart.isAwaitingApproval">Request a Quote</a>

Also, you are using AngularJS instead of Angular 2+.此外,您使用的是 AngularJS 而不是 Angular 2+。 I would recommend moving into Angular 2+ (currently at 8 version) to get the best of type support with Typescript.我建议使用 Angular 2+(目前为 8 版本)以获得 Typescript 的最佳类型支持。

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

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