使用Jquery和Regex在单击时删除单词

使用Jquery和Regex在单击时删除单词,jquery,regex,Jquery,Regex,我在寻求帮助。我发现这个脚本解决了我50%的问题,我想点击一个单词,然后使用jquery删除这个单词,类似下面的例子。如果使用下面的示例,它将突出显示您单击的单词 <!DOCTYPE html> <html> <head> <style> p { color:blue; font-weight:bold; cursor:pointer; } </style> <script type="text/javascript"

我在寻求帮助。我发现这个脚本解决了我50%的问题,我想点击一个单词,然后使用jquery删除这个单词,类似下面的例子。如果使用下面的示例,它将突出显示您单击的单词

<!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>
有没有人能帮我把它绑在一起,这样当我点击这个词时,它就会取代这个词。它也可以用[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>

var words=$(“p:first”).text().split(“”); var text=words.join(“”); $(“p:first”).html(“+text+”); $(“span”)。单击(函数(){ $('myfield').val($(this.remove().parent('p').text()); });
当您说“将结果更新到表单文本字段”时,我仍然不知道如何更新myfield中的值。您的意思是希望删除的单词在删除文本后转到表单文本字段或生成的段落?如果前者:

$("span").click(function () {
    $('#myfield').val($(this).remove().text());
});
编辑-参见下面的评论 如果后者:

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

谢谢Carpie,我用后一个例子更新了这个问题。我仍然不知道如何更新myfield以更新textarea中的文本。。。。我需要在文本区域上放置onclick事件吗?刚刚检查了您提供的第一个示例,它使用我单击的单词更新文本字段,并从段落中删除该单词。但是,第二个示例确实删除了

标记中的单词,但没有更新form.Oops中的文本字段。我有一个操作命令问题。在获取其父元素之前,我正在删除该元素。我更新了示例。看看这对你是否有效。
$("span").click(function () {
    var $p = $(this).parent('p');
    $(this).remove(); 
    $('#myfield').val($p.text());
});