我确信这非常简单,但我对JS很新,我想知道这样做的最好方法,而不是通过解决方法来解决它.
所以我有一个div块,当按下它上方的文本字段时,它的可见性就会被切换.
<h3 onclick="javascript: toggle1();">
<span style="cursor:pointer">
<b>Text1</b>
</span>
</h3>
<div id="Text1" hidden="hidden">blahblah</div>
Run Code Online (Sandbox Code Playgroud)
然后我有我的JS:
function toggle1() {
$('#Text1').toggle(1000);
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是当用户单击标题文本时,我还想更改其<hr>上方元素的高度
<hr id="line1" style="height:2px;border:none;color:#03930f;background-color:#03930f;" />
Run Code Online (Sandbox Code Playgroud)
我试过添加:
if($('#Text1').is(':visible')) {
document.getElementById("line1").style.height = "15px";
}
else {
document.getElementById("line1").style.height = "2px";
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用...我假设因为toggle()函数没有切换is(':visible')条件检查的相同的东西.
这样做的正确方法是什么?
警告信息说:
"警告:只能更新已安装或安装的组件.这通常意味着您在未安装的组件上调用了setState,replaceState或forceUpdate.这是一个无操作."
代码导致它:
async componentDidMount() {
const token = await AsyncStorage.getItem('google_token');
if (token) {
this.props.navigation.navigate('feed');
this.setState({ token });
} else {
this.setState({ token: false });
}
}
Run Code Online (Sandbox Code Playgroud)
经过一些谷歌,我真的很困惑,如果我应该担心这个警告.如何在不禁用此Github问题建议的规则的情况下获取警告消息?
我有这段代码检查用户是否已在Firebase中登录,如果是,请使用Redux调度操作并将状态更新为当前的auth用户.
/**
* check to see if the user has signed in already or not
*/
function initAuth(dispatch) {
return new Promise((resolve, reject) => {
const unsubscribe = firebase.auth().onAuthStateChanged(
authUser => {
dispatch({ type: "INIT_AUTH", payload: authUser });
unsubscribe();
resolve();
},
error => reject(error)
);
});
}
initAuth(store.dispatch)
.then(() => render())
.catch(error => console.error(error));
Run Code Online (Sandbox Code Playgroud)
我感到困惑的是,为什么在取消订阅中调用unsubscribe()?我知道你可以像在JavaScript递归中那样做,但这里有什么用?谢谢!
我正在尝试编写代码来分割一个没有标点符号的句子.例如,如果用户输入"Hello, how are you?",我可以将句子拆分为['hello','how','are','you']
userinput = str(raw_input("Enter your sentence: "))
def sentence_split(sentence):
result = []
current_word = ""
for letter in sentence:
if letter.isalnum():
current_word += letter
else: ## this is a symbol or punctuation, e.g. reach end of a word
if current_word:
result.append(current_word)
current_word = "" ## reinitialise for creating a new word
return result
print "Split of your sentence:", sentence_split(userinput)
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码工作,但如果我把一个句子没有用标点符号结尾,最后一个单词将不会显示在结果中,例如,如果输入是"Hello, how are you",结果将是['hello','how','are'],我想这是因为没有标点符号告诉代码字符串结束,有没有办法让程序检测到它是字符串的结尾?因此,即使输入"Hello, how are you",结果仍然是['hello','how','are','you'].
javascript ×3
async-await ×1
css ×1
firebase ×1
html ×1
jquery ×1
python ×1
react-native ×1
reactjs ×1
recursion ×1
redux ×1