Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取div的值,向其添加字符,然后将格式化后的值发送到Jquery中的另一个div_Jquery_Html - Fatal编程技术网

如何获取div的值,向其添加字符,然后将格式化后的值发送到Jquery中的另一个div

如何获取div的值,向其添加字符,然后将格式化后的值发送到Jquery中的另一个div,jquery,html,Jquery,Html,我试图首先获取div的值,然后在有空格的值中添加字符,如(+、-、%)等。完成后,我需要在另一个div上设置这个值。在jquery中是否可以这样做。这可能吗?我有点困惑 目前,我可以使用以下方法获取div的值: $(document).ready ( function () { $("#btn1").click ( function () { alert ( $(".eventLocation").html() ); }); }); alert用于检查我是否实际获得的值。这很好,但是如果值有

我试图首先获取div的值,然后在有空格的值中添加字符,如(+、-、%)等。完成后,我需要在另一个div上设置这个值。在jquery中是否可以这样做。这可能吗?我有点困惑

目前,我可以使用以下方法获取div的值:

$(document).ready ( function () {
$("#btn1").click ( function () {
alert ( $(".eventLocation").html() );
});

});
alert用于检查我是否实际获得的值。这很好,但是如果值有空格,我不太确定如何添加字符。例如,如果div中的值为“RH3 3HF”,则必须在“RH3”和“3HF”之间的空格处添加字符“+”。然后,必须将此格式化值转换为另一个div


如果您对此有任何建议,我将不胜感激,因为我不太确定如何继续。

这将满足您的需要

var text = $("#Div1").text();//get the text of the first DIV
text = text.trim();//trim the text to remove any leading and trailing whitespace
text = text.replace(/\s/g, "+");//replace all whitespace values with a "+"
$("#Div2").text(text);//set the text value of the second DIV

根据您的评论…

假设Div1只有一个链接,您可以这样做来获取href值

var href = $("#Div1 A").attr("href");
$("#Div2").html('<iframe scr=' + href + '></iframe>');
var href=$(“#Div1 A”).attr(“href”);
$(“#Div2”).html(“”);
试试这个:

var myText = $('.myDiv').html();
myText.replace(" ", "yourCharacterHere");

$('.myOtherDiv').html(myText);

请记住,
.html
也可以包含标签,如
等感谢所有的建议,我成功地实现了以下功能:

var text = $(".eventLocation").text();//get the text of the first DIV
text = text.trim();//trim the text to remove any leading and trailing whitespace
text = text.replace(/\s/g, "+");//replace all whitespace values with a "+"

$(".eventMap").append('<iframe width="190px" height="180px" frameborder="0" 
scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&
amp;hl=en&amp;ttype=&amp;q=' + text + '&amp;ie=UTF8&amp;output=embed&amp;&iwloc=near">
</iframe>');
var text=$(“.eventLocation”).text()//获取第一个DIV的文本
text=text.trim()//修剪文本以删除任何前导和尾随空格
text=text.replace(/\s/g,“+”)//将所有空白值替换为“+”
$(“.eventMap”).append('
');

这对用“+”字符替换空白有什么帮助?啊,我的错,我为你的标题制定了解决方案:/这不是我的标题;)替换为只替换第一个空格字符,不替换任何后续字符-需要使用全局标志(g)执行正则表达式这是一个很好的示例,最后在将文本值设置为第二个div之前,我如何将文本值放在?你喜欢这个工作吗$(“#DIV2”)。附加(“”)????谢谢你可以做<代码>$(“#Div2”).html(“”)谢谢,这很有道理。最后,如果第一个div包含一个链接,它将“”如何删除文本“sample link”以及a href的开头和结尾标记,因此只保留www.google.com。这样就可以在第二个div的iframe标记中设置它了???也许这就是我应该问的开始。。。Thanks@DanielPerez:是的,你可能应该问这个问题。请参阅我编辑的答案a href不会为空,即将显示以下内容。当文本“Go to Google”设置为iframe src标记内的第二个div时,是否可以去掉该文本以使链接正常工作???这是必需的,还是会按原样工作。。。谢谢