javascript - 通过使用jquery将php字符串传递给onclick函数来获取[object Object]
问题描述
我想提醒你好,而不是我得到 [object Object]。通过使用jquery将php字符串传递给onclick函数来获取[object Object]
<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<p id="demo"></p>
<a class="readmore" onclick="myFunction('<?php echo "hello";?>')">Readmore....</a>
<?php
call();
function call(){
?>
<script>
$(document).ready(function(){
$(".readmore").click(function(link){
alert(link);
});
});
</script>
<?php }
?>
</body>
</html>
解决方案
问题是您的 JavaScript 代码。您正在调用一个不存在的函数。这就是为什么它什么都不做。如果您想在 jQuery 中执行此操作,则必须定义链接。否则,link 等于 [object Object],因为 link 是由一个函数传递的,该函数在没有属性 link 的情况下调用。
固定的:
<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<p id="demo"></p>
<a class="readmore" onclick="show_alert('<?php echo "hello";?>')">Readmore....</a>
<?php
call();
function call(){
?>
<script>
function show_alert(link) {
alert(link);
}
</script>
<?php }
?>
</body>
</html>
使用 Jquery:
<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<p id="demo"></p>
<a class="readmore" link="test">Readmore....</a>
<?php
call();
function call(){
?>
<script>
$(".readmore").click(function(){
link = $(".readmore").attr("link");
alert(link);
});
</script>
<?php }
?>
</body>
</html>
推荐阅读
- angular - 如何在api中传递两个参数来更新数据?
- redux-observable - 循环一定次数
- c++ - QT 中 QDoubleSpinBoxes 的多个范围
- c - 编写一个名为 CalculateOddEven 的 C 函数,它接受一个指向整数 a 和实际整数 b 和 c 的指针
- python - 用逗号将字符串拆分为重复字符串的新行
- php - WordPress 显示链接标签,最多 4 个
- oracle - 从 wsdl 将安全标头传递给肥皂信封
- python - 如何修复pycharm中的这个错误?cv2.error:OpenCV(3.4.2)错误:(-215:断言失败)
- openid - IdentityServer4 和 Tableau Server OpenID 授权
- typescript - 错误:从 LocalStorage 获取项目时“类型 'string | null' 不可分配......”