社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

jQuery从函数发送和接收数据

m12l • 6 年前 • 1832 次点击  

我试图创建一个函数来替换一些字母,这样我就可以在不同的地方调用同一个函数。到目前为止这是我创造的。无法使函数成为返回主代码的数据。

$(document).ready(function() {
    MyFunction("Hello");
    alert(text);
});

function MyFunction(text) {
		var text = text.replace("h","m");
    return text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51378
文章 [ 4 ]  |  最新文章 6 年前
Kwame Opare Asiedu
Reply   •   1 楼
Kwame Opare Asiedu    6 年前

忽略区分大小写

function MyFunction (text) {
    return text.replace(/h/i, "m");
}
Adrian Brand
Reply   •   2 楼
Adrian Brand    6 年前
replace("h","m");

你想用m代替h

replace("H","m");
SherylHohman Presmelito
Reply   •   3 楼
SherylHohman Presmelito    6 年前

因为没有小的 h 在世界上。而是找一个大的 H :

$(document).ready(function() {
      let text = MyFunction("Hello");
      console.log(text);
});

function MyFunction(t) {
    let text = t.replace("h","m");
    return text
}

或者改用regex忽略该情况:
t.replace(/h/ig,"m")

SherylHohman Presmelito
Reply   •   4 楼
SherylHohman Presmelito    6 年前

将函数调用放入警报或将其设置为变量,然后如果要替换大写 H ,使用 小时 不 h 在你的职责范围内。

$(document).ready(function() {    
    alert(MyFunction("Hello"));
});

function MyFunction(text) {
		var text = text.replace("H","m");
    return text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>