javascript для переключения класса span на отображение / исчезновение. я вообще не знаю javascript [duplicate]

Хорошо, чтобы начать, когда вы это сделаете:

print(jiskya(2, 3))

Вы получаете что-то в значительной степени эквивалентное этому:

print(print(2))

Итак, что происходит? print(2) распечатывает 2 и возвращает None, который печатается внешним вызовом.

def hello():
    return 2

Если вы это сделаете:

print(hello())

Вы получаете 2, потому что, если вы распечатываете функцию, получите значение return. (Значение return обозначается символом return someVariable.

Теперь, хотя print не имеет круглых скобок, подобных большинству функций, это функция, немного отличающаяся в этом отношении. print return? Nothing. Поэтому, когда вы print print someVariable, вы получите None как вторую часть, потому что возвращаемое значение печати - None.

Так, как утверждают другие:

def jiskya(x, y):
    if x > y:
        print(y)
    else:
        print(x)

Необходимо переписать:

def jiskya(x, y):
    if x > y:
        return y
    else:
        return x
59
задан Roko C. Buljan 28 March 2016 в 03:33
поделиться

6 ответов

Посмотрите на jQuery Toggle

HTML:

<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>

jQuery:

jQuery(document).ready(function(){
    jQuery('#hideshow').live('click', function(event) {        
         jQuery('#content').toggle('show');
    });
});

Для версий jQuery 1.7 и более новое использование

jQuery(document).ready(function(){
        jQuery('#hideshow').on('click', function(event) {        
             jQuery('#content').toggle('show');
        });
    });

Демо

58
ответ дан Zared619 29 August 2018 в 00:34
поделиться

Вы можете использовать следующее:

mydiv.style.display === 'block' = (mydiv.style.display === 'block' ? 'none' : 'block');

-1
ответ дан 4444 29 August 2018 в 00:34
поделиться
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#hideshow').click(function(){
    $('#content').toggle('show');
  });
});
</script>

И html

<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>
0
ответ дан Edwin Martin 29 August 2018 в 00:34
поделиться

Чистый JavaScript:

var button = document.getElementById('button'); // Assumes element with id='button'

button.onclick = function() {
    var div = document.getElementById('newpost');
    if (div.style.display !== 'none') {
        div.style.display = 'none';
    }
    else {
        div.style.display = 'block';
    }
};

СМОТРИТЕ ДЕМО

jQuery:

$("#button").click(function() { 
    // assumes element with id='button'
    $("#newpost").toggle();
});

СМОТРЕТЬ ДЕМО

120
ответ дан Piyush Gupta 29 August 2018 в 00:34
поделиться

Вот простой способ Javascript:

<script>
  var toggle = function() {
  var mydiv = document.getElementById('newpost');
  if (mydiv.style.display === 'block' || mydiv.style.display === '')
    mydiv.style.display = 'none';
  else
    mydiv.style.display = 'block'
  }
</script>

<div id="newpost">asdf</div>
<input type="button" value="btn" onclick="toggle();">
19
ответ дан Roko C. Buljan 29 August 2018 в 00:34
поделиться

Вот как я скрываю и показываю контент с помощью класса. изменение класса на ничего изменит отображение на блок, изменение класса на «a» покажет отображение как none.

<!DOCTYPE html>
<html>
<head>
<style>
body  {
  background-color:#777777;
  }
block1{
  display:block; background-color:black; color:white; padding:20px; margin:20px;
  }
block1.a{
  display:none; background-color:black; color:white; padding:20px; margin:20px;
  }
</style>
</head>
<body>
<button onclick="document.getElementById('ID').setAttribute('class', '');">Open</button>
<button onclick="document.getElementById('ID').setAttribute('class', 'a');">Close</button>
<block1 id="ID" class="a">
<p>Testing</p>
</block1>
</body>
</html>
0
ответ дан Shawn Baldwin 29 August 2018 в 00:34
поделиться
Другие вопросы по тегам:

Похожие вопросы: