2010-07-17 29 views
1

我在尋找一些幫助。我發現這個腳本能夠解決我的問題50% - 我想單擊一個單詞並使用類似於下面示例的jquery刪除單詞。如果使用下面的示例,它會突出顯示您單擊的單詞。用Jquery和Regex刪除單詞

<!DOCTYPE html> 
<html> 
<head> 
    <style> 
    p { color:blue; font-weight:bold; cursor:pointer; } 
    </style> 
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script></head> 
<body> 

<p> 
    Once upon a time there was a man 
    who lived in a pizza parlor. This 
    man just loved pizza and ate it all 
    the time. He went on to be the 
    happiest man in the world. The end. 
</p> 
<script> 
    var words = $("p:first").text().split(" "); 
    var text = words.join("</span> <span>"); 
    $("p:first").html("<span>" + text + "</span>"); 
    $("span").click(function() { 
    $(this).css("background-color","yellow"); 
    }); 
</script> 
</body> 
</html> 

如何通過點擊刪除/刪除單詞。

我還發現這個例子用一個詞來代替一個詞。

var el = $('#id'); 
el.html(el.html().replace(/word/ig, "")); 

有沒有人可以幫助我把它綁在一起,這樣當我點擊單詞時,它就會替換單詞。它也可以用[Delete]之類的東西代替這個詞,我可以在表單提交時用正則表達式去掉它?

好,我已經有點想通了通過更換

$("span").click(function() { 
     $(this).css("background-color","yellow"); 
     }); 

$("span").click(function() { 
    $(this).remove(); 
    }); 

這使我的下一個問題。

如何將結果更新爲表單文本字段?

從頂端更新下面的例子

<form id="form1" name="form1" method="post" action="spin1.php"> 
    <p><%=(Recordset1.Fields.Item("g_article_desc_spin").Value)%></p> 
<textarea name="myfield" id="myfield" onFocus="this.blur();" cols="45" rows="5"></textarea> 
<script> 
    var words = $("p:first").text().split(" "); 
    var text = words.join("</span> <span>"); 
    $("p:first").html("<span>" + text + "</span>"); 
$("span").click(function() { 
    $('myfield').val($(this).remove().parent('p').text()); 
}); 
</script> 
     <input type="submit" name="button" id="button" value="Submit" /> 
    </form> 

我仍然不知道當你說「更新的結果,一個表單文本字段」如何在MyField的

回答

2

更新值做你的意思是你想刪除的文字被刪除後轉到表單文本字段或生成的段落?如果是前者 - :

$("span").click(function() { 
    var $p = $(this).parent('p'); 
    $(this).remove(); 
    $('#myfield').val($p.text()); 
}); 
+0

感謝Carpie我已經更新了後者的例子問題

$("span").click(function() { $('#myfield').val($(this).remove().text()); }); 

編輯看後下方 評論。我仍然不知道如何更新myfield來更新textarea中的文本....我是否需要在textarea上放置onclick事件? – 2010-07-17 13:08:00

+0

剛剛檢查過您提供的第一個樣本,它會使用我單擊的單詞更新文本字段,並從段落中刪除該單詞。然而,第二個示例刪除了

標籤中的單詞,但不更新表單中的文本字段。 – 2010-07-17 13:24:37

+1

糟糕。我有一個操作問題的順序。在獲取父項之前,我正在刪除該元素。我更新了這個例子。看看是否適合你。 – carpie 2010-07-17 13:30:56