我们可以使用以下两种方法在类似数组的对象上实现迭代:
let arrayLike = document.getElementsByClassName('dummy');
[].forEach.call(arrayLike, (e) => {
console.log(e);
});Run Code Online (Sandbox Code Playgroud)
<div class = "dummy">Test1</div>
<div class = "dummy">Test2</div>Run Code Online (Sandbox Code Playgroud)
或者使用slice先将类似数组的对象转换为数组:
let arrayLike = document.getElementsByClassName('dummy');
Array.prototype.slice.call(arrayLike).forEach((e) => {
console.log(e);
});Run Code Online (Sandbox Code Playgroud)
<div class = "dummy">Test1</div>
<div class = "dummy">Test2</div>Run Code Online (Sandbox Code Playgroud)
哪一个更可取,为什么在我不需要转换后的类似数组的对象的情况下呢?第一个感觉有点“ hacky”,但是第二个感觉更具可读性。还是两者都一样,这意味着两者都可以吗?
我正在嵌套 Promise,我必须知道嵌套的 Promise 是被拒绝的 Promise 还是已完成的 Promise,才能知道是否触发外部 Promise 链的 catch。为了区分嵌套 Promise 是被拒绝还是被履行,我throw在嵌套 Promise 中使用 acatch来表示拒绝;throw当我的嵌套 Promise 中没有时,总是表明履行catch。请看下面的例子:
let test = new Promise((resolve, reject) => {
resolve(42);
}).then((e) => {
console.log(e);
return new Promise((resolve, reject) => { //Error happens inside this nested Promise
resolve(32);
}).then((e) => {
console.log(e);
//Run other processes that may lead to an error
}).catch((e) => { //I added this catch block for error detection, whatever error it might be …Run Code Online (Sandbox Code Playgroud)好的,所以我曾经在 Python 中制作了一个非常简单的井字游戏,它很有趣,所以我想我会加点趣味并用 HTML/JS/CSS 来制作它更漂亮。进展非常顺利,但是在为游戏制作简单的“AI”时,我在路上遇到了一些障碍。游戏的简单 AI 应该是完全随机的,确实如此,但有时它会占据一个已经被占用的方格,有时它根本不移动,所以我知道这可能很简单,我只是不知道是什么。我认为它可能在这段代码的某个地方,但我不知道:
function computerEasyModeMove(){
if(player == 'x'){
computer = 'o';
}
if(player == 'o'){
computer = 'x';
}
var computerMove = Math.floor(Math.random() * 9) + 1;
while(computerMove == 1 && (topLeftBox.innerHTML == 'o' || topLeftBox.innerHTML == 'x')){
computerMove = Math.floor(Math.random() * 9) + 1;
}
while(computerMove == 2 && (topMiddleBox.innerHTML == 'o' || topMiddleBox.innerHTML == 'x')){
computerMove = Math.floor(Math.random() * 9) + 1;
}
while(computerMove == 3 && (topRightBox.innerHTML == 'o' || topRightBox.innerHTML == 'x')){ …Run Code Online (Sandbox Code Playgroud) 我遵循材料设计关于如何进行颜色主题的指南。我们可以更改一些已经定义的属性来自定义某个组件的颜色和/或形状。与颜色主题相关的一些有用的属性包括:(colorSurface例如改变组件的填充颜色Material Card)、colorSecondary(例如改变FAB的填充颜色)以及colorOnSecondary(例如改变FAB图标的填充颜色)。
目前,我有两个资源目录:values和values-night分别用于浅色主题和深色主题的 UI。它们控制分配给颜色属性的颜色值。代码:
<!-- Light-themed UI color values -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#546e7a</color>
<color name="colorPrimaryVariant">#29434e</color>
<color name="colorOnPrimary">#ffffff</color>
<color name="colorSecondary">#d81b60</color>
<color name="colorSecondaryVariant">#a00037</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorSurface">#ffffff</color>
</resources>
<!-- Dark-themed UI color values -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#546e7a</color>
<color name="colorPrimaryVariant">#29434e</color>
<color name="colorOnPrimary">#ffffff</color>
<color name="colorSecondary">#d81b60</color>
<color name="colorSecondaryVariant">#a00037</color>
<color name="colorOnSecondary">#000000</color>
<color name="colorSurface">#000000</color>
</resources>
<!-- Style resource file that changes in accordance to current default night …Run Code Online (Sandbox Code Playgroud) android android-theme android-studio material-design material-components-android
为什么当您反复更改包含在父视图中的子视图的可见性并测量父视图时,Android 会返回错误的结果?
我创建了一个简单的测试:一个只有一个 ConstraintLayout 和两个 TextView 的 XML 文件。我会反复将最后一个 TextView 的可见性更改为 GONE 和 VISIBLE。每次我改变 TextView 的可见性时,我都会测量 ConstraintLayout 的宽度和高度。为了改变 TextView 的可见性,我在 ConstraintLayout 上设置了一个点击监听器。
这是上述简单布局的 XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/iWillShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/colorOnPrimary"/>
// This one will be hidden on click
<TextView
android:id="@+id/hideMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iWillShow"
android:textColor="@color/colorOnPrimary" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在我的 Kotlin 文件中,我有以下内容:
testLayout.setOnClickListener {
val matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec((it.parent as View).width, View.MeasureSpec.EXACTLY)
val wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) …Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-viewgroup android-measure
根据文档,我只需要android:forceDarkAllowed=true在我的活动清单中设置并从parent="Theme.MaterialComponents.DayNight". 我试过了,但没有用。
这是我的清单文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:forceDarkAllowed="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
这是我的styles.xml风格:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.DayNight">
...
</style>
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码获取活动的主题名称:
resources.getResourceName(
packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
.applicationInfo.theme
)
Run Code Online (Sandbox Code Playgroud)
它显示我com.example.name:style/AppTheme而不是AppTheme.Dark。我怎样才能让它在我运行应用程序时MainActivity自动设置为使用AppTheme.Dark(即暗模式)使用android:forceDarkAllowed?
代码没有typedef(并且有效):
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用typedef双链接列表中的"节点"结构编写代码,但这不起作用:
typedef struct {
int data;
Node *next;
Node *prev;
} Node;
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题typedef?
我有以下轮播:
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
...
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="Images/pastry3.jpg" alt="First slide">
</div>
</div>
...
</div>
Run Code Online (Sandbox Code Playgroud)
我有以下 CSS:
.carousel {
width: 100%;
height: 100%;
}
.carousel .carousel-inner, .carousel .carousel-item {
width: 100%;
height: 100%;
}
.carousel-item img {
object-fit: cover;
object-position: 50% 50%;
}
Run Code Online (Sandbox Code Playgroud)
我的图像比屏幕大得多(宽度:100% 和高度:100%),我希望它们能够img在位于屏幕中心时保持其纵横比,因此object-fit: cover;和object-position: 50% 50%,但它不工作。为什么?
我正在尝试制作一个全屏练习主页,其中包含不同幻灯片中不同文本区域的滑动图像/视频。
给定一个数组中的一系列随机数,例如:147, 95, 254, 78, 66, 120,当它们(按位和)有效时,如何从索引n到索引找到操作数的结果?m&
例如:
n = 2和m = 5.这意味着您必须&从第二个元素(95)到第五个元素()的数组中的数字66.
我不知道除了&一个接一个之外如何,但这并不高效.我正在使用的问题有多个查询,这意味着对于一系列随机数(例如上面的),问题可能会多次询问元素中&数字的结果,n直到m每个查询包含不同值的元素中n和m.
编辑:我尝试过以下操作:将所有数字更改为二进制位并将所有位存储在deques中.然后,检查给定范围中的一个位是否为0,如果有一个0,则所述位返回0值.我确实得到了正确的答案.但是,它仍然不够有效.这是我的代码
android ×4
javascript ×3
css ×2
html ×2
android-view ×1
bootstrap-4 ×1
c ×1
carousel ×1
es6-promise ×1
logic ×1
material-components-android ×1
object-fit ×1
promise ×1
structure ×1
typedef ×1