0
我有以下解決方案,將單擊的單詞添加到<input>
字段字符串中。將點擊的單詞添加到字符串2
不過,我想改變的JavaScript讓我:
- 保持文字,我手動添加到
<input>
領域。目前它被覆蓋。 - 排除從
<p>
傳輸文本句號,以<input>
HTML
<p id="target_para">
Here's an example of the thing you wanted to be made clickable.
</p>
<input type="text" id="display" />
JS
(function() {
"use strict";
var para, targets, display, words, clickHandler, updateList, i, j, cur;
display = document.getElementById("display");
para = document.getElementById("target_para");
// Wrap every word in a span element
para.innerHTML = '<span>' + para.innerText.replace(/ /g,'</span><span> ') + '</span>';
// Updated target
targets = para.getElementsByTagName("span");
words = [];
// Handler for clicking a clickable element
clickHandler = function() {
var text = this.innerText || this.textContent,
idx = words.indexOf(text);
if (words.indexOf(text) < 0) {
// If not already in list, add it
words.push(text);
} else {
// Otherwise remove it
words.splice(idx, 1);
}
updateList();
};
// Update display of word list
updateList = function() {
while (display.firstChild) {
display.removeChild(display.firstChild);
}
// Set the input box value
display.value = words.join(",");
};
// Bind event handlers to clickable elements
for (i = 0, j = targets.length; i < j; i++) {
cur = targets[i];
cur.addEventListener("click", clickHandler, false);
}
}());
*你嘗試過什麼?* –
我明白,要保持手動輸入到''字段中的文本是什麼,是根本,是基於先前的反饋。 –
排除完全停止是我嘗試使用'g /./'實現的,但沒有成功 –