我有一个可编辑的 oneline 标题,如下所示:
<h1 class="headline" contenteditable="true" [textContent]="headline.name" (input)="headline.name=$event.target.textContent" (keydown.enter)="submit();false">
然而,当我点击输入时,输入字段会保持其焦点。 如何让它失去对输入的关注?
答案 0 :(得分:8)
正如yurzui在评论中指出的那样:
(keydown.enter)="$event.target.blur();submit();false"
答案 1 :(得分:0)
如果您想通过键盘输入事件触发模糊事件,只需在函数中传递#include <iostream>
#include <utility>
#include <vector>
// Returns a pair of closest less than (first) and greater
// than (second) numbers in 'vec' to that of 'number'.
// If a less than or greater than number can't be found,
// the value of 'number' is returned for that slot.
//
// Time complexity: O(N)
// Space complexity: O(1)
auto closestNumbers(const std::vector<int> &vec, int number) {
std::pair closest_numbers{number, number};
// Find closest below.
for (auto e : vec) {
if (e < number &&
(e > closest_numbers.first || closest_numbers.first == number)) {
closest_numbers.first = e;
}
}
// Find closest above.
for (auto e : vec) {
if (e > number &&
(e < closest_numbers.second || closest_numbers.second == number)) {
closest_numbers.second = e;
}
}
return closest_numbers;
}
int main() {
const auto v = {10, 115, 15, 35, 55, 75};
auto closest_numbers{closestNumbers(v, 40)};
std::cout << closest_numbers.first << " " << closest_numbers.second
<< "\n"; // 35 55
closest_numbers = {closestNumbers(v, 130)};
std::cout << closest_numbers.first << " " << closest_numbers.second
<< "\n"; // 115 130
closest_numbers = {closestNumbers(v, 1)};
std::cout << closest_numbers.first << " " << closest_numbers.second; // 1 10
}
并在函数中传递$event
。
例如:$event.target.blur();
然后在功能上添加
<div (keyup.enter)="getControl(value,$event)"></div>