2010-07-17 31 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