if display = block使用javascript将其更改为none

时间:2018-04-25 20:03:00

标签: javascript html css colors

代码

#element1{display=block}
<div id="element1"></div>

如何编写像

这样的javascript代码
otherbutton on click if(element1display=block){
    change it to display=none
}

5 个答案:

答案 0 :(得分:1)

function initMap() { geocoder = new google.maps.Geocoder(); var iconBestaandeklant = { url: 'http://icons.iconarchive.com/icons/iconshock/vista-general/32/house-icon.png', }; var Arnhem = { title: '<strong>Naam</strong><br>\ Dienst', lat: 51.986847, long: 5.955350, }; var locations = [ [Arnhem.title, Arnhem.lat, Arnhem.long, 0], ]; var map = new google.maps.Map(document.getElementById('map'), { }); var infowindow = new google.maps.InfoWindow({}); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map, icon:iconBestaandeklant, }); google.maps.event.addListener(marker, 'click', (function (marker, i) { return function () { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } document.getElementById('mode').addEventListener('change', function() { routeplannen(); }); routeplannen() function routeplannen() { var aanvraag = "6826AV"; var dienstverlener = "6827AV"; var directionsDisplay = new google.maps.DirectionsRenderer({ map: map }); var selectedMode = document.getElementById('mode').value; var request = { destination: aanvraag, origin: dienstverlener, travelMode: google.maps.TravelMode[selectedMode] }; if (request.travelMode == "DRIVING") {vervoersmiddel = "met de auto"} else if (request.travelMode == "BICYCLING") {vervoersmiddel = "met de fiets"} else if (request.travelMode == "TRANSIT") {vervoersmiddel = "met het openbaar vervoer"} else{vervoersmiddel = "lopend"} var directionsService = new google.maps.DirectionsService(); directionsService.route(request, function(response, status) { if (status == 'OK') { // Display the route on the map. directionsDisplay.setDirections(response); } }); directionsService.route( request, function( response, status ) { if ( status === 'OK' ) { var point = response.routes[ 0 ].legs[ 0 ]; $( '#travel_data' ).html( '<strong>Verwachte reistijd ' + vervoersmiddel + ": " + point.duration.text + ' (' + point.distance.text + ')</strong>' ); } }); } }

&#13;
&#13;
style.backgroundColor = "red"
&#13;
changeColor = function(){
  // Get the element by ID
  var div = document.getElementById("square");
  
  // Get the styles applied to that element
  var style = window.getComputedStyle(div)
  
  // Compare background color using rgb value
  if(style.backgroundColor == "rgb(0, 0, 255)")
    div.style.backgroundColor = "red";
}
&#13;
#square{
  width: 50px;
  height: 50px;
  background-color: blue;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

对要检索的元素使用.css()方法。

var theColorIs = $('element').css("color"); //Which will return the color in RGB.

if(theColorIs  == 'blue') {
      $("element").css({"color": "red"}); 
}

https://www.w3schools.com/jquery/jquery_css.asp

答案 2 :(得分:0)

你可以试试这个

&#13;
&#13;
$(document).ready(function() {
    $('.btn').on('click', function() {
        var textElement = $('.txt').get(0);
        var currentColor = textElement.style.color;
    
        if (currentColor === "blue") {
            textElement.style.color = "red";
        } else {
            textElement.style.color = "blue";
        }
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="txt" style="color:blue;">This is my text!</p>
<button class="btn">Change color</button>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

假设您有办法选择要检查的文本,并且所有文本都在一个元素中,您可以使用window.getComputedStyle来确定元素使用的color。然后,您可以使用if语句检查它是否是您要定位的颜色,然后为该元素分配不同的color。以下示例适用于Chrome;我不知道其他浏览器是否会完全返回rgb(0, 0, 255),但是,一旦你检查了它们返回的内容,扩展if语句以接受这些字符串应该是相当简单的。

document.querySelector('button').addEventListener('click', ()=> {
  let p = document.querySelector('.blue-text');
  pStyle = window.getComputedStyle(p);
  console.log(pStyle.color)
  if (pStyle.color === "rgb(0, 0, 255)") {
    p.style.color = "red";
  }
})
.blue-text {
  color: blue;
}
<p class="blue-text">I'm some text</p>

<button>Change Text Color</button>

答案 4 :(得分:0)

试试这个,

&#13;
&#13;
document.querySelector("button").addEventListener("click", changeDiv);
function changeDiv(){
	var element = document.getElementById("element1");
    element.style.display = (element.style.display == 'none') ? 'block' : 'none';
}
&#13;
#element1{display:block}
&#13;
<div id="element1">Sample</div>
<button>Click here</button>
&#13;
&#13;
&#13;